64,662
社区成员
发帖
与我相关
我的任务
分享
#ifdef WIN32
#define TEST_FILE "c:\\test.txt"
#else
#define TEST_FILE "/tmp/test.txt"
#endif
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void test()
{
int len;
char* buf;
//fstream sfs;
//sfs.open(TEST_FILE);
fstream sfs(TEST_FILE);
sfs.seekg (0, ios::end);
len = sfs.tellg();
sfs.seekg (0, ios::beg);
buf = new char[len];
sfs.read(buf, len);
cout << buf << endl;
delete []buf;
sfs.close();
}
int main()
{
fstream in_out;
in_out.open("c:\\test.txt",fstream::app);
if (!in_out)
{
cerr<<"con't open!"<<endl;
return -1;
}
string message;
getline(cin,message);
in_out<<message;
in_out<<endl<<endl;
test();
system("pause");
return 0;
}
void test()
{
fstream sfs(TEST_FILE);
sfs.seekg (0, ios::end);
int len = sfs.tellg();
sfs.seekg (0, ios::beg);
char *line = new char[len];
while(!sfs.eof())
{
sfs.getline(line,len);
cout << line << endl;
}
delete [] line;
sfs.close();
}