feof (end of file) not recognized.

Started by
3 comments, last by Cat_B 21 years, 3 months ago
Hi, I wrote a random access data file using fwrite. I then read it doing while(!feof){} but it doesn''t see the feof and keeps going. Do I have to write a special feof character? I know its ctl-z for windows, but how do I write this at the end of a file? thanx (I''m using C, not c++ and its a console program)
Advertisement
feof is a function. Your code is testing whether its address is not 0. Which it will never be. You need to actually call the function : while( !feof( fp ) ) { /* ... */ } where fp if your file pointer.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Yes, soory, that was an ommision in my post, but not my code.

my code is
while (!feof(cfPtr)){
fread(&dx,sizeof(dx),1,cfPtr);
fseek(cfPtr,sizeof(dx),SEEK_CUR);
fread(&dy,sizeof(dy),1,cfPtr);
printf("dx: %d /t dy: %d \n", dx, dy);
fseek(cfPtr,sizeof(dy),SEEK_CUR);
}
You don''t need to seek after a read. You''re probably seeking past the end of the file. I don''t know what feof returns in that case.
-Mike
Ah! Thank you

This topic is closed to new replies.

Advertisement