in what position is a file opened?

This is a discussion on in what position is a file opened? within the C++ Programming forums, part of the General Programming Boards category; I do this Code: ifstream codefin; ofstream codefout; while (not_annoyed) { codefout.open("bar"); // write to bar, works fine codefout.bar(); system("command ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    14

    in what position is a file opened?

    I do this

    Code:
    ifstream codefin;
    ofstream codefout;
    
    while (not_annoyed)
    {
        codefout.open("bar");
        // write to  bar, works fine
        codefout.bar();
        system("command to change bar to foo");
    
        codefin.open("foo");
        if (!codefin.is_open())
            cout<<"ERROR: couldn't open foo for reading"<<endl;
        if (codefin.eof()) 
             cout<<"FRACK!\n";
        while (!codefin.eof())
            getline(codefin,text);
         codefin.close();
    }
    The result is "FRACK!" the second time I run this code (it is done in a while loop)
    Why? Shouldn't the file be opened in the beginning? The file "foo" is changed before each run, but that shouldn't matter, should it?
    Last edited by grepawksed; 01-21-2006 at 12:56 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    I'm guessing that the first time you read through the input file you reach eof and the stream's relevant flag gets set and stay's set even when the file is closed and later reopened in the second iteration of the loop. When you check eof this time, the flag is still 'true' and thus you get the "FRACK" output. You need to clear the stream's flags each time through the loop. I'd also suggest not testing for eof in the while loop and instead testing the return result from the getline call directly:

    Code:
    while( getline(codefin,text) );
    codefin.close();
    codefin.clear();
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    14
    I think the clear() part solved it! Great! Thank you!
    Now, what difference is it when I put the getline in the while loop? In what way is your suggestion better?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Sometimes you will get an error when reading in a file (not very often with getline). If you are using eof() to control your loop and there is an error, eof() will not return true, and the loop will go on forever. Also, if there is an extra newline in your file (which there almost always is), and you read with operator >> instead of getline, then eof() will not return true until you read the trailing newline. This often leads to the while loop executing one extra time. Again, this problem doesn't happen if you use a single getline exclusively in your loop.

    In this example there should be any problems (unless you get some read error), but it is good practice to not use eof() to control the loop so that when you use more complicated code to read in the file you won't have any problems. Putting the getline inside the while loop won't hurt, but it might help in this case and will almost certainly help later. If you use operator >> to read from the file you should use that as the while control as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21