CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2004
    Posts
    5

    fstream issues with ios::app

    Having an issue with ios::app. I'm using the Xcode application on my Mac to compile it. Here's the code:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    char buffer[250];
    fstream myfile;
    myfile.open("testtext", ios::out | ios::trunc);
    	if(myfile.is_open())
    	{
    	myfile << "This is output \n";
    	myfile.close();
    	}
    myfile.open("testtext", ios::in);
    myfile.getline(buffer, 100);
    cout << "The file contains    " << buffer << endl;
    myfile.close();
    myfile.open("testtext", ios::app);
    myfile << "Hey this is YET another line of output \n";
    myfile.close();
    myfile.open("testtext", ios::in);
    myfile.getline(buffer, 100);
    cout << "The file contains    " << buffer << endl;
    myfile.close();
    
    return 0;
    }
    When the compiled program runs:

    The file contains This is output
    The file contains


    It leaves a blank after the second output. Why? I'm a beginner at learning C++ but I'm pretty sure that the second output should display both things written to the file right? Because "app" was supposed to "append" the line to the file therefore making two lines? So, yeah, I'm confused. Really would like some help on this please.

  2. #2
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762
    Don't explicitly close your fstreams, let them fall out of scope. Also, I think you have to do a fstream.clear() in order to open (or re-open) a file.

    Why are you using an old C-Style string and why are you using a member getline() function?

  3. #3
    Join Date
    Feb 2004
    Posts
    5
    Originally posted by kasracer
    Don't explicitly close your fstreams, let them fall out of scope. Also, I think you have to do a fstream.clear() in order to open (or re-open) a file.

    Why are you using an old C-Style string and why are you using a member getline() function?
    Just following what my c++ book says to do.

  4. #4
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762
    Originally posted by modyouup
    Just following what my c++ book says to do.
    Burn it

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    When you use an "ifstream()" , ios::in is automatically added,
    even if you do not specify it. If you open an "ofstream()",
    ios::out is automatically added as the mode. If you use
    "fsteam()", there is nothing automatically added. You are
    using fstream, so the only modes present are what you
    explicitly specify.

    When you open to append :

    Code:
    myfile.open("testtext", ios::app);
    The only mode is "ios::app". This is illegal. If you use ios::app,
    you must have ios::out. For an ofstream this is automatic, but
    not for an fstream. So you need to add it :

    Code:
    myfile.open("testtext", ios::out | ios::app);

  6. #6
    Join Date
    Feb 2004
    Posts
    138
    I also want to add in to make a question,

    Philip, could Philip tell me when I have to use ios::bin, I have a book but it doesnt tell me anything but an example to ask me to figure it out myself, please help...

    Thanks Philip

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    1) to the original question ... also, the second time you
    open to read, you only read 1 line, even though there
    will be 2 lines in the file. You can loop to get all the lines:

    Code:
    while (myfile.getline(buffer, 100))
    {
        cout << "The file contains    " << buffer << endl;
    }
    myfile.close();
    
    return 0;
    2) as for when to use ios::bin it's hard to have a hard-and-fast
    rule. When obvious time to use binary is when reading
    files such as BMP , AVI , JPG files.

  8. #8
    Join Date
    Feb 2004
    Posts
    138
    Originally posted by Philip Nicoletti
    2) as for when to use ios::bin it's hard to have a hard-and-fast
    rule. When obvious time to use binary is when reading
    files such as BMP , AVI , JPG files.
    Thanks Philip a lot,

  9. #9
    Join Date
    Feb 2004
    Posts
    5
    Thanks! Still a little problem though. This is my current code:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    char buffer[250];
    fstream myfile;
    myfile.open("testtext", ios::out | ios::trunc);
    	if(myfile.is_open())
    	{
    	myfile << "This is output \n";
    	myfile.close();
    	}
    myfile.open("testtext", ios::in);
    myfile.getline(buffer, 100);
    cout << "The file contains    " << buffer << endl;
    myfile.close();
    myfile.open("testtext", ios::out | ios::app);
    myfile << "Hey this is YET another line of output \n";
    myfile.close();
    myfile.open("testtext", ios::in);
    myfile.getline(buffer, 100);
    while (myfile.getline(buffer, 100))
    {
    cout << "The file contains    " << buffer << endl;
    }
    myfile.close();
    
    return 0;
    }

    When I run the program:

    The file contains This is output
    The file contains Hey this is YET another line of output


    Shouldn't the second output show both lines? Yet it shows only the second line. odd
    Last edited by modyouup; February 18th, 2004 at 01:34 PM.

  10. #10
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    You do an initial getline() before the while loop, so that
    line never gets printed. Remove that line.

    Code:
        myfile.getline(buffer, 100);  // remove this line
        while (myfile.getline(buffer, 100))
        {
            cout << "The file contains    " << buffer << endl;

  11. #11
    Join Date
    Feb 2004
    Posts
    5
    Originally posted by Philip Nicoletti
    You do an initial getline() before the while loop, so that
    line never gets printed. Remove that line.

    Code:
        myfile.getline(buffer, 100);  // remove this line
        while (myfile.getline(buffer, 100))
        {
            cout << "The file contains    " << buffer << endl;
    Removed line, now it gives this when I run...is this correct?:

    The file contains This is output
    The file contains This is output
    The file contains Hey this is YET another line of output


    Didn't expect it to say "the file contains" three times.

  12. #12
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Yes that is correct. getline() reads one line in the file (where
    the line delimiter is '\n') - it does not read the entire file.

    You can sometimes "trick" getline into reading an entire
    text file at once by putting the delimiter character (the
    third argument which is '\n' by default) to some character
    not found in the text file. NULL is a good example, since
    most "normal" text files will not have it. I modified the
    second read section below (and put a new line after
    the "The file contains" for readability :

    Code:
        myfile.open("testtext", ios::in);
        while (myfile.getline(buffer, 100, 0))
        {
            cout << "The file contains :\n" << buffer << endl;
        }
        myfile.close();
    But the "normal way" to do it would be :

    Code:
       myfile.open("testtext", ios::in);
    
        cout << "The file contains :\n";
        while (myfile.getline(buffer, 100))
        {
            cout << buffer << endl;
        }
    
        myfile.close();
    or even :

    Code:
        myfile.open("testtext", ios::in);
        cout << "The file contains :\n" << myfile.rdbuf() << endl;
        myfile.close();

  13. #13
    Join Date
    Feb 2004
    Posts
    5
    OK, so in it's normal operation, getline will always just read one line. So if there's more than one read, it'll always repeat itself and put the rest on the next line. Mmmm, ok got it. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured