By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

File Append operation in c++

100+
P: 105
Hi all,

I have to create a text file containing a set of results and I have used as
ofstream fout("results.txt,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.
Aug 6 '07 #1
Share this Question
Share on Google+
5 Replies


RRick
Expert 100+
P: 463
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::app);

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.
Aug 7 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
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:

Expand|Select|Wrap|Line Numbers
  1. ofstream f("data.txt", ios_base::out | ios_base::beg);
  2.  
Aug 7 '07 #3

P: 93
Hi all,

I have to create a text file containing a set of results and I have used as
ofstream fout("results.txt,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.
Aug 8 '07 #4

100+
P: 105
The problem is that I have called ofstream("results.txt",ios::out); 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
Aug 16 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
Did you not read Post #3 and Post #4 ??
Aug 16 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.