473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Can I append text to a file?

The following program opens a text file and wants to append new text
to it, but everytime I run it, it returns me with "open file fail!".
Can anybody help?
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
fstream outfile("test.txt", ios_base::in | ios_base::out |
ios_base::app);
if (!outfile)
{
cout << "open file fail!\n";
exit(1);
}

char ch;

//display original text
outfile.seekg(0);
while (outfile.get(ch))
cout << ch;
cout << endl;

//append input text
outfile.seekp(ios_base::end);
while (cin.get(ch))
outfile << ch;

//display new text
cout << endl;
outfile.seekg(0);
while (outfile.get(ch))
cout << ch;
cout << endl;

system("pause");
return 0;
}

Jul 19 '05 #1
2 19889
gukn9700 wrote:
The following program opens a text file and wants to append new text
to it, but everytime I run it, it returns me with "open file fail!".
Can anybody help?
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
fstream outfile("test.txt", ios_base::in | ios_base::out |
ios_base::app);
This combination of file modes is not allowed; consequently the file
open attempt fails. FWIW, when specifying append mode, the only legal
ios_base mode combinations are,

out | app
binary | out | app

[see Table 92 in ISO/IEC 14882:1998]

if (!outfile)
{
cout << "open file fail!\n";
exit(1);
DO NOT use the C library function 'exit()' to terminate a C++ program. I
realize that C programs commonly use exit() to terminate themselves, but
using exit() in a C++ program is asking for trouble. To learn the
specific reasons why you should not use exit() in a C++ program, locate
any good book / FAQ on the C++ programming language.

FWIW, if you want/need to respond to error conditions in a C++ program,
you should use C++'s exception handling mechanism to report/handle these
conditions. [Research the following C++ keywords: try, catch, throw].
}
[remainder of code snipped]


--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #2
I know why now!
everytime a stream reaches its end it can no long input or output or
even seek position. To make it work we should first clear() it.

Like:
outfile.clear();

That clear the EOF flag and make it work again.

following code works:

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
fstream outfile("test.txt", ios_base::in | ios_base::out);
if (!outfile)
{
cout << "open file fail!\n";
return -1;
}

char ch;

//display original text
while (outfile.get(ch))
cout << ch;
cout << endl;

//append input text
outfile.clear(); //must clear before seeking!
outfile.seekp(0, ios_base::end);
while (cin.get(ch))
outfile << ch;

//display new text
cout << endl;
outfile.clear(); //must clear before seeking!
outfile.seekg(0);
while (outfile.get(ch))
cout << ch;
cout << endl;

outfile.close();
system("pause");
return 0;
}
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Amjad | last post by:
How can I append a text file to another? Amajd
2
by: Roy Gourgi | last post by:
Hi, How could I append a text file to another text file. Let's say that I have a File1 and I want to append File2 at the end of File1, how would I do that? TIA Roy
3
by: Macca | last post by:
Hi, I am writing some information to a text file using the TextWriter Class. However I am having problems appending data to a file that already exists and already has text in it. I just seem...
3
by: Luke | last post by:
I'm pretty stuck at the moment and wondering if anyone can spot the problem. Trying to create a function that will read a text file into a list and return that list. I wrote the following...
9
by: andy.z | last post by:
If 2 people try to access the same text file at the same time to write to it - what happens in PHP ? What I mean is - presumably the first will be ok - But what will the second person actually...
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
2
by: sajidali | last post by:
i am trying to append a text file using c#. when i executes my application i writes to the text file using more then one functions. i want to append file only during execution of programe. next time...
1
by: wish | last post by:
Hello, I have problem is when i append new data in text file always append at the last of the text file. Am i can append the new data in beginning of the text file? Mean when i have update data...
2
by: Andez | last post by:
I've wrote a windows service that performs simple functions within our application. To ensure safe running of the service - if it errors we want to know where when how - it logs to a text file - in...
1
by: Deshi | last post by:
Hi,. I am trying to append text into the RTF file, nothing seems to be working for me... here is the code FileStream fs = new FileStream("c:\\djj.rtf", FileMode.Append, FileAccess.Write);...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
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,...

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.