C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 16th, 2006, 12:38 AM
Izkata Izkata is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 51 Izkata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 44 m 41 sec
Reputation Power: 10
Ifstream's eof()

I've heard a few times that ifstream's eof() function shouldn't be used due to some innacurracy or unexpected behavoir, and that there was another way to check the file, but couldn't remember what it was. Any help?

ex:
ifstream Read;
char bar[21];
Read.open("foo.txt", ios::in);
if (!Read.eof()) Read.get(bar, 20);
Read.close();

Reply With Quote
  #2  
Old January 16th, 2006, 01:58 AM
jafet jafet is offline
Redpill
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2005
Posts: 1,660 jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 11 h 19 m 55 sec
Reputation Power: 150
Send a message via MSN to jafet
Before bad() testing...

cpp Code:
Original - cpp Code
  1.  
  2. ifstream Read("filename"); // ios:in is default anyway so this is redundant
  3. if(!Read.is_open())
  4. {
  5.     cout << "Cannot open file!";
  6. }
  7. if(Read.bad())
  8. {
  9.     cout << "Bad file!";
  10.     Read.close();
  11. }
  12. if(Read.eof())
  13. {
  14.     cout << "Empty file!";
  15. }

Reply With Quote
  #3  
Old January 16th, 2006, 02:07 AM
Mazzie's Avatar
Mazzie Mazzie is offline
Making a mistake is not sin.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Finland
Posts: 406 Mazzie User rank is Captain (20000 - 30000 Reputation Level)Mazzie User rank is Captain (20000 - 30000 Reputation Level)Mazzie User rank is Captain (20000 - 30000 Reputation Level)Mazzie User rank is Captain (20000 - 30000 Reputation Level)Mazzie User rank is Captain (20000 - 30000 Reputation Level)Mazzie User rank is Captain (20000 - 30000 Reputation Level)Mazzie User rank is Captain (20000 - 30000 Reputation Level)Mazzie User rank is Captain (20000 - 30000 Reputation Level)Mazzie User rank is Captain (20000 - 30000 Reputation Level)  Folding Points: 32554 Folding Title: Starter FolderFolding Points: 32554 Folding Title: Starter Folder
Time spent in forums: 1 Week 1 Day 6 h 20 m 50 sec
Reputation Power: 208
Send a message via MSN to Mazzie
I do not know any unexpected behaviour related to eof(). But, you could also use Read.good(), which also detects other errors, not just eof. Anyways, I think you should first read from the stream, and then check if the EOF was encountered. If you first check for EOF, and then read, nothing says that the thing you read after the check is not EOF.

IE, perhaps trying:

if(Read.good()) Read.get(bar...
if(Read.eof()) //previous get returned EOF...

If you miss that check after reading from file, and then try use what you read, it may not work as expected (in case the file ended).

mm.. I hope I am clear... Although at this hour it seems quite impossible to me to be understandable :/

Reply With Quote
  #4  
Old January 16th, 2006, 11:49 AM
Izkata Izkata is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 51 Izkata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 44 m 41 sec
Reputation Power: 10
Bah.. It was after midnight and I didn't feel like typing the whole thing - here's a better example of what I meant:

Code:
ifstream Read;
char bar[21];
Read.open("foo.txt", ios::in);
while(!Read.eof())
{
   Read.get(bar, 20);
   Read.ignore(100, '\n');
   cout << bar << endl;
}
Read.close();


I'll remember .good(), .bad(), and .is_open(), though, and see how they work for me.. Thanks!

Reply With Quote
  #5  
Old January 16th, 2006, 01:14 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
Comments on this post
Mazzie agrees: But doesn't the checking for EOF AFTER the get() also prevent from that error to occur?
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.

Reply With Quote
  #6  
Old January 16th, 2006, 02:49 PM
Izkata Izkata is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 51 Izkata User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 44 m 41 sec
Reputation Power: 10
...That's exactly what I was looking for!

(So, asking the whole question the exact right way does help )

Thanks for everyone's help!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Ifstream's eof()

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap