473,473 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

File Append operation in c++

105 New Member
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
5 2724
RRick
463 Recognized Expert Contributor
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
9,208 Recognized Expert Moderator Expert
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
Girish Kanakagiri
93 New Member
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
mickey22
105 New Member
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
9,208 Recognized Expert Moderator Expert
Did you not read Post #3 and Post #4 ??
Aug 16 '07 #6

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

Similar topics

2
by: adamsroy | last post by:
Hi I'm trying to append an already existent text file, simple enough, but it doesn't seem to be working, here's my code <%@LANGUAGE="JAVASCRIPT" CODEPAGE="1252"%> <% var stringToWrite =...
5
by: Phil Endecott | last post by:
Dear All, I have some code that first populates a std::set, and subsequently does a mixture of tests-for-presence and deletions. As I understand it, populating the set will be O(N log N), since...
4
by: Amy L. | last post by:
It is my understanding that in order to append XML to a file you would have to read the file in as an XML document and insert the node. However, what if the file is close to 1GB in size? Would you...
2
by: djc | last post by:
I'm hoping someone can point me in the right direction as to what I need to read up on in order to accomplish this: (what classes, methods, etc.. and/or general guidance) I want to provide a...
4
by: AHP | last post by:
Hi, I'm using Visual Studio 2005. I am developing a web application that uses the FileUpload control to upload text files to a directory on a webserver. This works fine. However, for me to be...
0
by: bhu | last post by:
hi, i need to read a File Line by Line do some work and then append to each line at the End Status and save the file. what approach should i take ? any idea ? I know to read a file , write a file...
5
by: fazulu deen | last post by:
HI all, I have to write a C program ,suppose if i want to tranfer 1MB from host memory to device say SD ,first i have to split it into 5kbytes after having splitted into 5kbytes each, not more...
3
by: srinivasanr | last post by:
I want to roll back file delete operaion using c#. How can i do it? I am deleting file using this code, File.Delete(fileName);
1
by: laxmis | last post by:
Hi I have a question, How does PHP read a file. I know that by using PHP file handle we can write or read a file. but how the file handle operates when file handle is used
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.