文件操作

使用c++的fstream头文件中的两个类完成文件操作:ofstream(写入)  ifstream(读出),简单且方便。

示例代码:

View Code
 1 #include <iostream> 
2 #include <fstream>
3 #include <string>
4 using namespace std;
5
6 void main()
7 {
8 ofstream ofs("E:\\xx.txt"); // 创建写入文件
9 ofs << "sss" << endl;
10
11 cout << "this is a current sample" << endl;
12
13 ifstream ifs("E:\\xx.txt"); // 关联要读出的文件
14 string str;
15 while(ifs >> str)
16 {
17 cout << str << endl;
18 cout << "换行" << endl;
19 }
20 }

 

posted on 2012-04-08 10:41  笔记吧... 可能只有自己看得懂  阅读(186)  评论(0编辑  收藏  举报