[C] 파일 읽는거 마지막 줄이 중복 출력 되네요
글쓴이: stargt / 작성시간: 금, 2006/01/20 - 5:57오후
#include <stdio.h>
main()
{
FILE *fp;
char s[256];
int i = 1;
fp = fopen("abc_.txt", "r");
if(fp == NULL)
return;
while(feof(fp) == 0){
fgets(s, 255, fp);
printf("%04d: %s", i, s);
i++;
}
fclose(fp);
}
위의 코드대로 작성한다음
abc_.txt 파일을
abcdefg[엔터] hijklmn[엔터] opqrstu[엔터] vwxyz[엔터]
이렇게 한후 실행했더니 결과가 원래
0001: abcdefg 0002: hijklmn 0003: opqrstu 0004: vwxyz
이렇게 나와야 정상인데
0001: abcdefg 0002: hijklmn 0003: opqrstu 0004: vwxyz 0005: vwxyz
이렇게 나옵니다.
그래서 abc_.txt 파일의 마지막에 있는 [엔터]를 제거했더니 원하던데로 나오고요...
그런데 진짜 책에는 끝에 엔터 넣으라고 나와있는데;
이거 또 성안당(출판사) 책이 이상한건가요? 그게 맞으면 성안당 책에서 코드 오류 6번째 발견한거 되는데;
Forums:


[code:1]#include <string.h>
#include <string.h> ... while(feof(fp) == 0){ memset(s, 0, 256); fgets(s, 255, fp); printf("%04d: %s", i, s); i++; } ...fgets()를 사용하기 전에 memset()으로 버퍼를 초기화시켜 주시면 어떨까요?
-----
http://monpetit.posterous.com/
http://monpetit.tistory.com/
[url=http://www.cinsk.org/cfaqs/html/nod
C FAQ 12.2 참고하세요.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Korean Ver: http://cinsk.github.io/cfaqs/
흠... 감사합니다
흠... 감사합니다
#include <stdio.h>
int main()
{
while(1)
printf("0");
return 0;
}
댓글 달기