1

Yo guys,

I'm trying to read a text file:

while(!feof(fp)){ 

    fscanf(fp, "%c", &c);

    printf("%c", c);

}

But... the output always duplicate the last char, I dont know why... :/

Any idea?

Frank
  • 241
  • 1
  • 2
  • 8
  • Are you saying that the output of your program is the first character of the input file `fp` repeated? Is it [the first char] repeated once for every character in the input file? Is this really the entirety of your loop and I/O? What does your input file look like? Is `c` an `int` or an `unsigned char`? – Matthew Hall Nov 22 '12 at 23:46
  • c is a char... I'm reading a text file that looks like it "+1 2", but the number 2 appear duplicated when I use that code, dont know why. Anyway, the last char is always duplicated... – Frank Nov 22 '12 at 23:54
  • possible duplicate of ["while( !feof( file ) )" is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) – Thomas Padron-McCarthy Mar 30 '13 at 07:56

1 Answers1

0

I've wondered that too, finally I found an answer!

I could not explain it any better, so check out the link

Engineer
  • 1,436
  • 3
  • 18
  • 33