Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can I append text to a file?

Reply
Thread Tools

Can I append text to a file?

 
 
gukn9700
Guest
Posts: n/a
 
      07-16-2003
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: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;
}

 
Reply With Quote
 
 
 
 
Jim Fischer
Guest
Posts: n/a
 
      07-16-2003
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: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]
>


--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com


 
Reply With Quote
 
 
 
 
gukn9700
Guest
Posts: n/a
 
      07-17-2003
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: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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
append() to each row of a text file sylvaticus C++ 3 10-16-2007 04:35 PM
the address of list.append and list.append.__doc__ HYRY Python 10 09-26-2007 09:41 AM
"Insert" (not append) new text segment to an existing text file sm C++ 2 04-29-2005 03:55 PM
How to append to the start of text file? Rach Java 3 08-24-2004 05:58 AM
How to append to a text file Dilantha Seneviratne Java 10 01-09-2004 12:44 PM



Advertisments