这些仅仅是最基本的文件打开和关闭,文件读取和写入。
代码如下:
#include <iostream>
#include <fstream>
using std::ofstream;
using std::ifstream;
using std::endl;
using std::cin;
using std::cout;
int main()
{
bool m;
ofstream w("text.txt");
m=w.is_open();
cout<<m<<endl;
w<<"this is a word!"<<endl;
w<<"hi!"<<endl;
w.close();
ifstream r("text.txt");
m=r.is_open();
cout<<m<<endl;
int n=r.eof();
cout<<n<<endl;
while(1)
{
char buf[100];
r.getline(buf,100);
cout<<buf<<endl;
if(r.eof()==1)
break;
}
r.close();
return 0;
}
实例结果如下: