File Append operation in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey22
    New Member
    • Feb 2007
    • 105

    File Append operation in c++

    Hi all,

    I have to create a text file containing a set of results and I have used as
    ofstream fout("results.t xt,ios::out|ios ::app);
    where results.txt is the file I would like to put the data.

    When I run the code for the first time it is displaying the results in the file properly.But when I execute my program again it append new results to the old results as I am using ios::app.If i dont use ios::app I am not getting the output when I execute for the first time.

    How can I clear my initial results each time I run my program?Could anyone give me suggestions

    Thanks.
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    It sounds like it should work. For example:

    ofstream out;
    out.open( "t.dat", ios::out);

    Will open a file and truncate the file each time it is open.

    out.open( "t.dat", ios::out|ios::a pp);

    Will open a file and append info to the end of the file.

    If this is what your code looks like, I would look at your code more closely for other issues. Are you closing the file? Are you checking for errors? Try a real simple test to see if it works or not.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by Mickey22
      When I run the code for the first time it is displaying the results in the file properly.But when I execute my program again it append new results to the old results as I am using ios::app.If i dont use ios::app I am not getting the output when I execute for the first time.
      Don't use ios::app. Use ios::beg instead.

      That way each time you opne then file is written from the beginning.

      BTW, ios is the old form. You should be using ios_base::beg, etc. LIke this:

      [code=cpp]
      ofstream f("data.txt", ios_base::out | ios_base::beg);
      [/code]

      Comment

      • Girish Kanakagiri
        New Member
        • May 2007
        • 93

        #4
        Originally posted by mickey22
        Hi all,

        I have to create a text file containing a set of results and I have used as
        ofstream fout("results.t xt,ios::out|ios ::app);
        where results.txt is the file I would like to put the data.

        When I run the code for the first time it is displaying the results in the file properly.But when I execute my program again it append new results to the old results as I am using ios::app.If i dont use ios::app I am not getting the output when I execute for the first time.

        How can I clear my initial results each time I run my program?Could anyone give me suggestions

        Thanks.
        If you want to clear the initial results then
        use ios::trunc
        This clears your previous results. Make use of this only once at every execution. You can check this by setting a counter.

        Regards,
        Girish.

        Comment

        • mickey22
          New Member
          • Feb 2007
          • 105

          #5
          The problem is that I have called ofstream("resul ts.txt",ios::ou t); in a function say function1 and I have to call this function from function2 .

          Each time I call this function1 from function2 the above statement fstream is being called and ios::out is creating new output file.

          In case if I place ios::app I am not able to clear the contents when I run the program each time.

          In what way can I put the conditions more clearly?Could anyone please suggest.

          please let me know if my question is not clear.thanks

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Did you not read Post #3 and Post #4 ??

            Comment

            Working...