Can I append text to a file?

G

gukn9700

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::eek:ut |
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;
}
 
J

Jim Fischer

gukn9700 said:
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::eek:ut |
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]
 
G

gukn9700

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::eek:ut);
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top