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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why it does this? 1

Status
Not open for further replies.

rick05

Programmer
Feb 23, 2001
15
MX

hi guys...

I have a little trouble, I´m reading from a file with ifstram, since I want to review the format of this file...

however when I use file.oef() in my loop, my program read the last data two times! :-(( do U know what happens?

my code is:

¦float val1;
¦int fin;
¦
¦ifstream hoyos("hoyo");
¦
¦if (!hoyos.fail())
¦ {
¦
¦ while (!hoyos.eof())
¦ {
¦ cout << &quot;si se pudo abrir el archivo!\n&quot;;
¦ hoyos >> val1;
¦ cout << &quot;valor de val1: &quot; << val1 << &quot;\n&quot;;
¦ getch();
¦ };
¦

my trouble is if my file is :

1 2 3 4
5 6 7 8

my program lists:

1
2
3
4
5
6
7
8
8

two times the last one!

why??!!!

I hope U can help me... please...

rick


 
just change your while to this

while(!hoyos.eof() && hoyos.peek() != EOF)

eof is funky. I think it was meant to be used as a condition in a do-while loop.


Matt
 

hi Matt...

well, I tried your advise, however is the same :-(

I tried it with a do-while loop, and a while loop and did the same...

:-((

thx anyway.
 
Well... then my next assumption is this:

You dont reach the end of the file when reading in 8 and then when you try to read into val again nothing happens because there is no numerical value and it reaches eof. You may need to do a getline and use atoi. Im gonna cut and paste your code into a program and see what happens.

Matt
 
Here ya go

void main()
{


float val1;
int fin;

ifstream hoyos(&quot;hoyo.txt&quot;);

if (!hoyos.fail())
{

while (!hoyos.eof() && hoyos.peek()!= EOF)
{
cout << &quot;si se pudo abrir el archivo!\n&quot;;
hoyos >> val1;
cout << &quot;valor de val1: &quot; << val1 << &quot;\n&quot;;
hoyos.get();
}
}


}//end of main function

Matt
 
Your problem is not in findig the end of file. you are printing the value of the variable val1 after the end of file is reached. After the end of file the variable val1 will have the previous value and that is printed again, after the print ( cout ) only the you are checking the end of file.



¦
¦ while (!hoyos.eof()) // Here you are checking the end of file without reading anything from the file. You have to check this after the read operation only. Change this to an infinite loop like while(1)
¦ {
¦ cout << &quot;si se pudo abrir el archivo!\n&quot;;
¦ hoyos >> val1;
// end of file may will come. At that time val1 will have the previous value.

// put a if statement before the cout

if (hoyos.eof()) break;

¦ cout << &quot;valor de val1: &quot; << val1 << &quot;\n&quot;;

// that previou value will be printed (in the end of file case)
¦ getch();
¦ };
¦


Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top