
-----------------------------------
CodeMonkey2000
Sun Jan 20, 2008 9:12 pm

How do I read till the end of a file?
-----------------------------------
Suppose I am reading from a data file but I have no idea about its size. I tried doing something like this:
#include 
#include 

using namespace std;

int main()
{
	ifstream fin("foo.txt");
	int a;
	while(fin>>a)
	{
		//whatever
	}
}


But it doesn't work :(

-----------------------------------
HeavenAgain
Sun Jan 20, 2008 9:15 pm

RE:How do I read till the end of a file?
-----------------------------------
while (!fin.eof( ))  instead of while(fin>>a)  does that work? because you are checking if it is the end of the file, not if it have something.... thats a bit weird

-----------------------------------
CodeMonkey2000
Sun Jan 20, 2008 9:22 pm

RE:How do I read till the end of a file?
-----------------------------------
No, it still goes past the eof. Why doesn't this work?

-----------------------------------
OneOffDriveByPoster
Sun Jan 20, 2008 11:13 pm

Re: RE:How do I read till the end of a file?
-----------------------------------
No, it still goes past the eof. Why doesn't this work?Assuming that all the file reading code is there (none in the "whatever") and that your input is of the right form, I am not quite sure why it doesn't work (may be missing something obvious though).  If you did not type the input file yourself, make sure you try with something that you saved on the same machine with a reasonable text editor.  If you are using DOS/Windows editors, your file may have a DOS end-of-file character that is confusing your program.

-----------------------------------
CodeMonkey2000
Sun Jan 20, 2008 11:40 pm

RE:How do I read till the end of a file?
-----------------------------------
Never mind, I was actually dividing by zero. Strange though, my compiler kept saying that I have reached the end of file...

-----------------------------------
md
Mon Jan 21, 2008 1:03 am

RE:How do I read till the end of a file?
-----------------------------------
Compilers tell you they've reached the end of the file when there is a syntax error of some kind.

Programs generate EOF errors (or more usually segfaults) when they reach the end of files.

Terminology is key ;-)
