Thread: fstream ios:app troubles

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    fstream ios:app troubles

    I have searched through many pages and have yet to find an answer...

    with
    fstream myfile;

    This snippet writes the line and works properly

    myfile.open("test.txt", ios:ut);
    myfile << "This is my second entry.\n";
    myfile.close();


    This snippet erases everything in the file (it will not append)

    myfile.open("test.txt", ios::app);
    myfile << "This is my second entry.\n";
    myfile.close();

    I understood that fstream will let you use both ifstream and ofstream functions (including append) Why does this not work?

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Code:
    ofstream file("BLAH.txt",ios::app);
    WOW! LOOK WHAT I FOUND!

    http://www.cprogramming.com/tutorial/lesson10.html
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    I have already declared it as fstream...

    Here is the whole program -
    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    int main() 
    {
    
        
        char buffer[256];
        fstream myfile;
        
        myfile.open("test.txt", ios::out);
        myfile << "This is my first entry.\n";
        myfile.close();
        
        myfile.open("test.txt",ios::in);
        myfile.getline(buffer,100);
        cout << "The file originally contained:  " << buffer << "\n";
        myfile.close();
        
        myfile.open("test.txt", ios::app);// <--- There it is!!!
        myfile << "This is my second entry.\n";
        myfile.close();
        
        
        myfile.open("test.txt",ios::in);
        myfile.getline(buffer,100);
        cout << "The file now contains:  " << buffer << "\n";
        myfile.close();
        
        myfile.open("test.txt", ios::out);
        myfile << "This is my third entry.\n";
        myfile.close();
        
        myfile.open("test.txt",ios::in);
        myfile.getline(buffer,100);
        cout << "The file now contains:  " << buffer << "\n";
        myfile.close();
     
    return 0;
    }
    Why, exactly, will ios::in ios:ut work, but ios::app wil not?
    Last edited by Ken Fitlike; 10-04-2006 at 02:15 PM. Reason: added code tags

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    restated

    Let me restate, I don't think I can use a real ofstream now (to append), because myfile is already declared as a type fstream. The compiler won't let me cross use them.

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Ifstream is used for reading in files, ofstream is used to create them. You need to change your code, and read the link I gave you.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    Maybe I am just daft. The post you linked does not help. I am not using ifstream and ofstream... I am using fstream.

    The documentation that I am using says that:

    when "myfile" is declared as type "fstream" you are able to:

    myfile.open("whatever.txt",ios::in);
    myfile.open("whatever.txt",ios:ut);
    myfile.open("whatever.txt",ios::trunc);
    myfile.open("whatever.txt",ios::app);

    You see, I cant simply ofstream now, becuase "myfile" is of type fstream...

    Why do all the above examples work except the :app one?

  7. #7
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    I understand it's of type fstream. So just change it to 'ofstream' and it will work. Follow the link, unless you have to do it using fstream. I've never used fstream, so I can't help with that, ofstream always worked just fine for me.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    7
    Thank you much for your help. A very funny thing happened...


    when I changed:
    myfile.open("test.txt", ios::app);

    to:
    myfile.open("test.txt", ios:ut|ios::app);

    it started working. This completely wrecks me, as I can't see why. If I just use out, it erases everything else. If I just use app, it does nothing at all. When I use them both with an "|" it works.

    Again, thanks much. I am such a noob.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  2. Using fstream
    By Sridar in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2005, 12:36 PM
  3. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 01:10 PM
  4. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  5. Problems with fstreams.
    By mosdef in forum C++ Programming
    Replies: 7
    Last Post: 06-19-2002, 03:36 PM