#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
using namespace std;
int main()
{
int i=666;
ofstream out("a.txt");
if(out.is_open())
{
out<<"gaoxudong"<<endl;
out<<"lihail"<<endl;
out<<i<<endl;
结果: 在a.txt中写入:
// gaoxudong
// lihail
// 666
}
out.close();
char buffer[256];
ifstream in("a.txt");
if (! in.is_open())
{
cout << "Error opening file";
return -1;
}
while (!in.eof() ) //如果读文件到达文件末尾,返回true。
{
in.getline (buffer,100);
cout << buffer << endl;
}
in.close();
return 0;
}