LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-25-2008, 09:20 PM   #1
twelvenine
Member
 
Registered: Sep 2006
Posts: 72

Rep: Reputation: 16
using eof in c++


i have to write some code that reads from a file until it reaches the end. it looks kind of like this:
Code:
ifstream input;
input.open(inputfile);
while(!input.eof())
{
input >> string;
cout << string << endl;
}
the file that it's reading from looks like this:
Code:
aaa
bbb
ccc
ddd
but for some reason, the above while loop will print out:
Code:
aaa
bbb
ccc
ddd
ddd
i can't figure out what's making it loop the extra time.
 
Old 10-25-2008, 10:21 PM   #2
twelvenine
Member
 
Registered: Sep 2006
Posts: 72

Original Poster
Rep: Reputation: 16
ok, i did some research and it seems that this is happening because of how the eof flag gets set. apparently it only gets set when you try to do a read, but nothing is there. so, i guess i could change the while loop to:
Code:
input >> string;
while(!input.eof())
{
cout << string;
input >> string;
}
but is there some way that i could update the eof flag without doing a read and throwing off the flow of my reads from the file? like say some function that i could call at the end of the while loop like input.updateeof().
 
Old 10-25-2008, 10:54 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Sure -

Code:
#include <stdio.h>
#define BUFLEN 80
...
  char buff[BUFLEN];
  while (fgets (buff, BUFLEN, fp))
    fputs (buff, fp);
  ...
 
Old 10-25-2008, 11:30 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by twelvenine View Post
Code:
while(!input.eof())
It's better to use while (input) so that you also catch error states other than EOF.
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Eof tostay2003 Programming 4 07-05-2006 10:11 AM
EOF and \n exvor Programming 4 12-27-2005 02:20 PM
EOF finding Goblin_C_Noob Programming 7 09-10-2005 09:58 AM
finding the eof nodger Programming 12 11-26-2004 02:17 AM
<< Eof ? sikandar Linux - Software 5 09-18-2003 11:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration