文件读写,将文件内容按照字符读出,另外也可以将内容赋值给字符串输出。
#include <fstream>
#include <sstream>#include <iostream>
using namespace std;
int main()
{
cout<<"文件输入:hello world"<<endl;
ofstream ofile("f1.txt",ios::out);
ofile<<"hello world..";
ofile.close();
ifstream ifile("f1.txt",ios::in);
char ch;
cout<<"字符输出:"<<endl;
while(ifile.get(ch))
{
cout<<ch;
}
ifile.close();
ifstream infile("f1.txt",ios::in);
cout<<'\n'<<"字符串输出:"<<endl;
ostringstream ostr;
char c;
while(infile.get(c))
{
ostr.put(c);
}
cout<<ostr.str()<<endl;;
infile.close();
}