473,703 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ 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.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.
Aug 6 '07 #1
5 2740
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::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.
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.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.
Aug 8 '07 #4
mickey22
105 New Member
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
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
9833
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 = "date=12/0305atfuzionhouse&guest1=SomeName&mail1=somemail&phone1=Somephone22222"; var fso;
5
2420
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 each of the N inserts will be O(log N), and the test and deletes will each be O(log N). In this case, however, I am populating it with values in ascending order - it's actually a for loop. I feel that because of this it should be possible to...
4
3899
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 require > 1GB of RAM to deal with reading this file in efficiently? Also, is XML an optimal solution when files get this large? I know I can solve my issue by formatting the records contained in my program into a comma delimited format which...
2
1104
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 link on our intranet site that when clicked will copy a file from a server (not the server that IIS is runing on) to the client machine. This is a front-end file for a database system that is updated often. I want to provide users a way to easily...
4
3434
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 able to further process the files, I need to insert some data at the BEGINNING of the text file. All the solutions I've seen either append the text to the end of the file, or overwrite the data at the start of the file, or read the entire file into...
0
1007
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 but don;t know how to have a single handle to do both read and write. thanks bhu
5
1989
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 than that(spiliting can be uniform or random)...even i can have less than 5k.... I will use one file and make it executable,and dump fields specifying such as data transfer size,source address,destination address and next pointer in it.
3
4046
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
1353
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
8667
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9249
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9116
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9013
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7860
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3120
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 we have to send another system
3
2063
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.