Reputation: 824
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?
Upvotes: 1
Views: 205
Reputation: 6298
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
.
Upvotes: 1
Reputation: 438
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;
Upvotes: 5