[완료] fgets가 이상합니다. (제가 이상할지도 ㅠ)

kww1023의 이미지

#include
#include
#include //exit()

#define BUF 100

void error_handling(char *message);

int main(void)
{
FILE *fp = NULL;
char buf[BUF];
char *ret = NULL;
int fputs_ret = 0;

memset((void*)buf, 0, sizeof(buf)*sizeof(char));

fp=fopen("C:\\fgets_test.txt","at");
if(fp==NULL)
{
error_handling("fopen error\n");
}

printf("Before fputs fp : %d\n",ftell(fp));
fputs_ret=fputs("KAAAAAAAAAA\n", fp);
if(fputs_ret<0)
{
error_handling("fputs error\n");
}
printf("After fputs fp : %d\n",ftell(fp));

printf("=================================\n");
printf("fputs ret %%d : %d\n",fputs_ret);
printf("fputs ret %%c : %d\n",fputs_ret);

printf("=================================\n");
printf("Before fgets fp : %d\n",ftell(fp));

ret=fgets(buf, sizeof(buf), fp);
if(ret==NULL)
{
error_handling("fgets ret is NULL\n");
}

printf("After fgets fp : %d\n",ftell(fp));

printf("=================================\n");
printf("ret %%p : %p\n", ret);
printf("buf %%s : %s\n", buf);

fclose(fp);

return 0;
}

void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}

=====================================================================================
fgets를 연습해보고 있는데요
왜 파일과 콘솔창에 이상한 한글이 출력될까요?
뭐가 잘못된 거죠?
전 아무리 봐도 잘 몰라서 질문 드립니다. ㅠ

mirr187의 이미지

질문하신 내용과는 다르지만...

fopen 사용시에 쓰신 "at" 에서 t 모드는 어떤거죠? ;; window에서 코딩 해본 경험이 없어서.. 혹시나 해서 여쭤봅니다...

man page 에 나와 있는 모드는...

r Open text file for reading. The stream is positioned at the beginning of the file.

r+ Open for reading and writing. The stream is positioned at the beginning of the file.

w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file,

가 다인데...

올려주신 코드로 테스트 해보면 fgets() 수행 이후 mode가 read에 대한 부분이 없기에... 위의 man page를 보시면 "a" 모드에서 지원하는건 writing at end of file입니다.. 그런 이유로 Bad File Description 에 해당하는 EBADF 가 발생하는군요... fopen의 mode를 한번 확인해보시기를 바랍니다.

kww1023의 이미지

우선 이렇게 답변 달아주셔서 감사합니다.
방금 막 해결을 하고 돌아왔습니다. 어제부터 뚫어져라 쳐다보니 오늘 뚫리는군요 ㅠ
(예상하신대로 window에서 작업을 하고 있습니다.)

r 모드로 바꾸니 제대로 동작을 하는군요.
fgets는 a mode나 w mode는 동작 하지 않는 거 같더라고요. 이유는 모르겠어요.
그래서 fputs에는 a mode로 열린 fp를 주고, fgets에는 r mode로 열린 fp를 줘서 성공 했습니다 !

참, 그리고 t mode는 txt 파일을 다루겠다라는 겁니다.

답변 주셔서 감사합니다. 오늘 하루도 좋은 하루 보내세요^^

planetarium의 이미지

윗분이 맨페이지에서 가져와주신대로
r은 읽기전용, w는 쓰기전용, a도 쓰기전용입니다. w나 a에선 읽기(fgets())가 불가능하죠.
읽기와 쓰기를 동시에 하려면 r+나 w+, a+ 를 쓰시면 됩니다.
(셋의 차이는 현재 파일의 존재 유무에 따른 처리와, 파일 내용의 어디서부터 쓰거나 읽기 시작할지의 차이입니다.)

텍스트 모드 t와 상대되는 옵션으로는 바이너리 모드 b가 있습니다.

kww1023의 이미지

a+ mode로 당장 다시 한번 시도해봐야겠습니다.
(멍청하게 fp를 두개나 할당하다니 ㅠㅠ)

답변 감사합니다.!!
좋은 하루 보내세요!!!

mirr187의 이미지

저도 덕분에 text mode와 binary mode가 있다는걸 알게 됐네요 감사합니다 ^^;

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.