472,354 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

File Append operation in c++

105 100+
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 2652
RRick
463 Expert 256MB
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 Expert Mod 8TB
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
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 100+
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 Expert Mod 8TB
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
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.