1

I have a file with the forwarded output of a cmd command. A C program loops though the characters until it reaches the end of the file. But unfortunately there are some EOF indicators in the file before it actually ends. The content of the file is:

Abbildname                PID      Sitzungsname     Sitz.-Nr.   Speichernutzung
========================= ======== ================ =========== ===============
System Idle Process              0 Services                   0             8 K
System                           4 Services                   0         5ÿ744 K
smss.exe                       440 Services                   0           276 K
csrss.exe                      616 Services                   0         1ÿ924 K
wininit.exe                    720 Services                   0           944 K
services.exe                   792 Services                   0         7ÿ892 K
lsass.exe                      800 Services                   0        14ÿ300 K
svchost.exe                    924 Services                   0           420 K
fontdrvhost.exe                952 Services                   0           680 K
svchost.exe                    960 Services                   0        23ÿ880 K
WUDFHost.exe                  1012 Services                   0         7ÿ416 K
svchost.exe                    484 Services                   0        14ÿ816 K
svchost.exe                    544 Services                   0         5ÿ196 K

Thats how I read the content of the file:

FILE *file;
file = fopen(completePath, "r");
char character;
while((character=getc(file))!= EOF){
    putchar(character);
}
fclose(file);

Any time when there is a 'ÿ' it is interpreted as EOF. How can I fix this so that I read in the whole file and stop when I reach the actual EOF?

TheProgrammer
  • 784
  • 1
  • 6
  • 20
  • post some code with how you're opening and reading the file. – Joe Jan 27 '18 at 21:39
  • I have added the reading method. – TheProgrammer Jan 27 '18 at 21:41
  • Both. It is there and it is what I get. I read from the file and any time I read in the ÿ (thats just displayed as ÿ in "editor") I get the numeric value fo -1. Thats End Of File. There is no logical explanation why it is there but it is... – TheProgrammer Jan 27 '18 at 22:07

2 Answers2

5

EOF is not an char value. It is integer value. It is guaranteed that no other character equals to it. However, you converted the value returned by getc (which is type of int) to char before comparison (which is undefined in this case). You shall modify char character; to int character;

1

You can use int feof ( FILE * stream );

Check end-of-file indicator Checks whether the end-of-File indicator associated with stream is set, returning a value different from zero if it is.

   while( (character=getc(file))!= EOF && !feof(file) )

Note:

The above code allows to use character to be type of char.

When character is type of int there is no need for feof(file) since the first condition would properly recognize the EOF.

Pablo
  • 13,271
  • 4
  • 39
  • 59
sg7
  • 6,108
  • 2
  • 32
  • 40
  • *The above code allows to use `character` to be type of `char`* I don't think so, the `(character=getc(file) != EOF` will still be wrong, you would still read an extra nonsense-char because `feof` will return true after the next `getc` call. See https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Pablo Jan 28 '18 at 23:41
  • @Pablo There is `&&` condition. `&& !feof(file)` should prevent the next `getc` call. – sg7 Jan 28 '18 at 23:50
  • No, read the link, or [this](https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284351&answer=1046476070). `feof` will return `NULL` after the end-of-file indicator is set, which happens gen `getc` reads again. Because `char != EOF` will fail, you are reading one more byte than you should. – Pablo Jan 28 '18 at 23:59