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: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;
}
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;
}