Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with finding End of File (EOF) 1

Status
Not open for further replies.

maximusAF

IS-IT--Management
Feb 11, 2003
6
0
0
US
Hello,
I am coding a program that inputs data from a file. I have a loop that will input each line and then output each line. I have the while loop set as:

while (! file.EOF())
{
...
)

For some reason, the loop will process and output the last record in the file twice. For example, if my file was like the following:

John_Doe 1234
Jane_Doe 4567
Fred_Flintstone 8901

The while loop will output it as this:

John_Doe 1234
Jane_Doe 4567
Fred_Flintstone 8901
Fred_Flintstone 8901

It seems that the program knows that it's the EOF, but for some reason it processes the last record twice. And since it processes it twice, of course it messes up the rest of my calculations in the program. What am I doing wrong here? Thanks.
 
Could you post your code? It would help me figure out the problem.
 
Make sure there are no spaces after your last name in this case:
Fred_Flintstone 8901

If there is even one space or a return it can cause this error. Usually how I resolve this problem is go to the very last line of my text file....to the last letter and delete until I cannot move the right arrow key at all past the last letter.
Sera
I often believe that...
My computer is possessed!
 
I think I figured it out. I fixed it when I changed it to this:

Read record from file
loop while NOT EOF
{
...
...
...

read next record from file
}

I originally did not have the first read before the loop. Do you always have to put the first read before the loop? That solved my issue, but just wondering if that is the normal convention.
 
Well I think if you did a
do
{

}while

you could avoid the first read outside of the loop....but I am not sure.

Sera
I often believe that...
My computer is possessed!
 
This doesn't seem to make a difference... sorry! I am looking into this further. If I can find a better solution I will post it. It is kinda kludgy to do what you are doing....I know I have done it many times myself...but now I am perplexed as to why it happens....hope to get an answer

Sera
I often believe that...
My computer is possessed!
 
ok here is my code....
for some reason when I read the input as a character it gives me an extra letter....but if I do it this way (use strings)it works.... Don't know why!!! Microsoft any answers? heh heh

Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
    //char getletter;
	ifstream infile(&quot;test1.dat&quot;);
    int count = 0;
	string getletter;
	do
	{
		infile>>getletter;
		cout<<getletter<<endl;
		count++;
    }while(!infile.eof());
    
	cout<<count<<endl;
	infile.close();
	return 0;
}

Sera
I often believe that...
My computer is possessed!
 
The explanation to this is quit simple,what is inside the loop is executed before the condition of the loop.

Lets say that we are using the folowing code for reading a file line by line:
Code:
FILE *fp = fopen(&quot;myfile.txt&quot;);
char buffer[100] = {0};
......
....

while( !feof(fp))
{
   fgets( buffer, 100, fp );
   cout<<buffer;
}
This formulation is incorrect because the changing of lines is made inside the loop so therefore before testing that we are at the end of the file the expressions inside the loop will be executed first.

The correct way to do it would be:
Code:
while(  fgets( buffer, 100, fp ) != NULL )
{
   cout<<buffer;
}

If we were to read the file one character at the time,instead of using the folowing code:

Code:
char c;

while( !feof(fp))
{
   c = getc(fp);
   cout<<c;
}
we should use something like this:

Code:
char c;

while( (c = getc(fp)) && !feof(fp))
{
   cout<<c;
}
 
Leibnitz,
Have you tested this...?
Sera
I often believe that...
My computer is possessed!
 
Well I'll be...it took me a sec to understand what you were doing....but it does work. Pretty Nice...seems obvious now!
---Star for you! Sera
I often believe that...
My computer is possessed!
 
more explanatory is this:

while(!feof(fp))
{
c = getc(fp);
if(!feof(fp))
{
cout<<c;
}
}

Sera:
> If there is even one space or a return it can cause this error

normally there MUST be end_of_line characters after the last line in text files. What can cause an error in some cases is namelly an absence of linefeed characters.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top