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;
}
实例结果如下:
代码如下:
#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;
}
实例结果如下:
相关文章推荐
- c++文件读写基本操作
- 文件打开关闭与读写等基本操作 C++
- linux -> c/c++ 之文件的基本操作
- C++基本文件操作
- C++读写txt文件(基本操作1)
- C C++ 文件基本操作
- c/c++文件的基本操作
- c++中文件操作的5中基本途径
- c++(MFC)对文件的基本操作
- C++基础知识(四)文件的基本操作
- C++中基本的文件操作
- C++中对文件进行读写操作
- UNIX 文件系统基本操作
- C++ 文件操作
- 文件的一些基本操作
- 在C++中实现文件的读写的基本方法
- Linux 基本命令(文件操作)
- [DLL]模仿C++中INI文件代码操作习惯的XML配置文件类
- c++中关于文件操作资料收集(值得收藏)
- C++文件操作之二 : C++ 的fstream类提供的文件操作