Thread: question about eof

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    255

    question about eof

    in the following code im copying how i keep seeing it online and in a book but all im getting is the dos windowing popping up and printing and infinte supply of nothing scrolling down forever

    im trying to get it to stop at the end of the file but its just going on forever any reason for this??? there must be one but i cant figure it out. this looks about the same way it was set up in C++.net and now i was trying to do it in regulare C++ and i cant figure out why its not working this time. help? and thanks!


    Code:
    char str[50];
    	ifstream in("yes.txt",ios::app);
    	while (!in.eof())
    	{
    		in.getline(str,50);
    		cout << str << endl;
    	}
    	
    	in.close();
    hooch

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    ios::app ... doesn't that start the stream at the end of the file? Perhpas eof() is not useful for reading in ios::app.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should not use eof() to control your loop. Use getline inside the while instead:
    Code:
    while (in.getline(str, 50))

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ah yes the ios::app seemed to be the problem thanks ios::in works

    and why do you say to use getline over eof()? any reason for that? i gather it works cause it saids getline while it still can and is true?
    hooch

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I guess that's a perfect example of what can happen when you use eof() as your loop condition.

    My book also uses eof() as the loop condition, but as members on this forum have explained to me, that is the wrong approach, and it's the reason your program gets caught in an infinite loop.

    This line:

    ifstream in("yes.txt",ios::app);

    is clearly something you took from example code that demonstrates how to append data while writing to a file. If you open a file and move to the end of it, then there is nothing to read. Apparently, your stream fails on the first read, and that failure is not caused by hitting the eof. Therefore, you end up in an infinite loop where the eof error flag is not set, yet you cannot read data from the stream because it has other errors.
    Last edited by 7stud; 05-03-2005 at 06:40 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are a couple reasons to use the return value of getline instead of eof().

    If the stream fails before the end of the file, then the eof bit will never be set and you will have an infinite loop. The return value of getline returns false if the eof or fail bit is set.

    Also, if you read in the last line with a trailing newline, then the call to getline succeeds, the eof bit is not set, and the loop continues. The next time through the getline call fails because there is no more data in the file, the eof bit is set, and no data is read into str. However, you output str anyway. This causes the last value of str to be output again.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    ah that makes sense eof needs a value and it doesnt have one with ios::app cause that starts at the end of the file i was thinking app was append like in .net which just starts at the beginning if there is a file otherwise it opens one...yes .net sucks horribly stupid school oh well lol

    so just always set the getline as the test value for reading which yea that makes sense cause Peek() in .net read till the end of the file and getline reads as long as it can making it true until proven false..ah makes sense

    thank you though is there a reason to use eof() then? with getline seemingly 100000x better?
    hooch

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    is there a reason to use eof() then? with getline seemingly 100000x better?
    Presumably for error testing. At the end of the while loop, you have no idea whether all the data was read in unless you check for the eof error flag:

    Code:
    if(!in.eof())
           cout<<"Uh oh, not all the data was read in.\n "
               <<"Maybe I should terminate or\n"
               <<"maybe I should reset the error flags with\n"
               <<"in.clear() and try to read the data in again, or\n"
               <<"maybe I should continue processing what data I have.\n"
    Last edited by 7stud; 05-04-2005 at 07:56 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yes. More complicated programs might want to know whether the read failed or just reached the end. Also, there are many different types of streams used in many different ways, some of which might require eof testing versus general fail testing.

  10. #10
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    why not get the best of both worlds

    Code:
    while (in.getline(str, 50) || !eof())
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    oh ok that works makes sense for me so eof is just an error checker nothing wrong with that

    so at the end of that while loop or rather outside of it just stick a eof test to see if it gets all the data read or not.

    i just thought of this in .net there is a String * variable and you can array it like you can an int or whatnot is there a way in regular C/C++ to array some kind of string and store data in a similar fashion. i know arrayed char's are strings so im kind of thinking you cant do this with a char and actually would need a String variable type to array it and store data like that?
    hooch

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There is no reason to check eof() in addition to the return value of getline or operator>>.

    The return value of the input functions checks for eof in addition to other bad states like fail(). So to check the return value and eof() is redundant. Just use the method that checks the return value of the input function (either getline or operator>>) and don't worry about eof() until you have to do serious error checking/reporting/handling.

    In C++ there is the string class which you will like much more than character arrays. You must #include <string> and make sure you qualify it with std:: or a using declaration.

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The C++ Standard Template Library has a string class (available by using the line #include <string> at the top of the program with your other includes, and making sure namespace std; is available by one approach or the other). You can also make an array of C style null terminated strings by using a two dimensional array of char.

    //STL string class
    string myStrings[3]; //array of three strings I'm not sure if this works because the length of each string isn't determined. So, see the following for something that will work.

    vector<string> myStrings; //a vector of strings. a vector is an array that you don't need to initialize to a given size. Well, it's more than that, but that's a quick and dirty description.

    //Not using the STL or other similar stuff, but going to the nuts and bolts, bottom line of the language.
    char myStrings[3][21]; //An array of 3 null terminated char arraysC style strings. Each string can hold up to 20 visible char and the null terminating char.
    Last edited by elad; 05-04-2005 at 10:56 AM.
    You're only born perfect.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> string myStrings[3]; //array of three strings I'm not sure if this works because the length of each string isn't determined. So, see the following for something that will work.

    This works fine. The string class manages its own length. In this case it starts off empty. However, using the vector of strings is probably more appropriate, especially for someone coming from .NET.

  15. #15
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    well the string mystrings[3]; way seemed to not like spaces and kept screwing up and not taking spaces too well any reason for this? same with the char nuts and bolts way they both take spaces as a end of the current array?

    is there a way to give these things spaces i thought they took spaces last time i checked? the regular char str[50]; takes spaces ok why dont the string arrays and the char str[3][40] take them ok?

    and for those curious i dont get the vector way there lol so didnt mess with it
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  2. Strange EOF happenings
    By fatinez in forum C Programming
    Replies: 6
    Last Post: 09-23-2003, 08:37 PM
  3. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM
  4. EOF question, scanf also
    By newbie123 in forum C Programming
    Replies: 4
    Last Post: 01-31-2003, 04:15 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM