您的位置:首页 > 编程语言 > C语言/C++

c++基本文件操作

2014-06-10 15:39 218 查看
这些仅仅是最基本的文件打开和关闭,文件读取和写入。

代码如下:

#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;

}

实例结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: