-
checking for end of file
Whether I use feof() or a function with EOF, I can't get my program to really check for the actual end of a .exe file because apparently there are a bunch of EOF characters throughout the file. Is there a better way to find the real end of file? I finally got my copy program to work doing this:
Code:
while ( (bytes = fread(buf, sizeof (char), BUFSIZ, readfile) ) != NULL)
{
fwrite(buf, sizeof (char), bytes, writefile);
}
and all my variables are defined correctly...
but it really doesn't check for end of file, it just prevents the program from writing extra characters to the end of the destination file... but without making my own function to find the real end of file, isn't there some built in function that will do the job?
-
How how are opening the file? Are you sure you are opening it as a binary file?
-
> but it really doesn't check for end of file,
Of course it does - that's what the comparison (which should be != 0 by the way) does.
If it's not working, you need to post more code.
-
I was using fopen() and that code was the one that finally worked for me. yea i changed that NULL to a 0, got rid of the compiler warning... but when I try to do a byte at a time, like byte = getc(readfile), then check for EOF, like
Code:
if (byte == EOF)
{
break; /*break out of for loop */
}
it finds an EOF character almost immediately. in one of my .exe files it found it after byte 12. then it stopped copying. like i said, the code i previously posted is what finally did work for me, but why doesn't this method work as well? or even instead of doing "if (byte == EOF)" using "if (feof(readfile) )"? neither way works the way i'm trying to use it because there is a character that the program interprets as EOF and is scattered throughout .exe programs. And by the way yes I am opening the file in binary "fopen(readfile, "rb");"
-
I'd be willing to bet byte is defined as a char. (Or perhaps unsigned, the end result is the same.) Right?
If you're testing a byte for EOF, you can't use a char. Use an int instead.
Quzah.
-
Yes, I am defining byte as a char, but why does that make a difference? isn't a char saved the same way as an int? and are you telling me if i just change it to an int it should work how I want? that'd be awesome...
EDIT: nevermind I'm dumb since I didn't read the FAQ sooner. Or at least find out what EOF is. I'm just reading about EOF in the FAQ now and that clears everything up. it's defined as an int and has a negative value... that's why it makes a difference. and i changed my code and it works great
-
You did use an int, and not a char for byte right?
Code:
int ch;
while ( (ch=getc(readfile)) != EOF ) {
}
> because there is a character that the program interprets as EOF and is scattered throughout .exe programs.
Which is why you store it in an int
EOF is a value distinct from every other possible value of a character (all 256 of them). If you store it in a character, then its bound to equal one of those characters at some point.
-
yea now byte is an int and it works a lot better, and i even understand why haha