Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3001786
  • 博文数量: 167
  • 博客积分: 613
  • 博客等级: 中士
  • 技术积分: 5473
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-13 21:35
个人简介

人, 既无虎狼之爪牙,亦无狮象之力量,却能擒狼缚虎,驯狮猎象,无他,唯智慧耳。

文章分类
文章存档

2015年(19)

2014年(70)

2013年(54)

2012年(14)

2011年(10)

分类: C/C++

2013-07-24 14:50:01

     对于文件的读写操作,C++提供了更为方便强大的模式功能,我们当然可以使用传统的C-FILE*模式打开文件,也可以使用Windows提供的CreateFile函数获得文件句柄,但是考虑到跨平台和简便性,还是要数C++的iostream类对象。iostream提供了对于标准输入输出、文件读写和内存string读写的全套机制,当然,这一切都建立在类树结构的继承-派生机制之上。关于C++的标准I/O库的资料网上有很多,今天自己就仅仅总结下自己认为比较重要的几个知识点。
1. 标准I/O库的组织结构:
     iostream类继承自ostream和istream,同时兼顾读取和写入,而ostream专门处理数据流的写入问题,派生出ofstream(文件)和ostringstream(string类型),istream则专门处理数据流的读取问题,派生出ifstream(文件)和istringstream(string类型)。
     标准I/O库只有三个头文件:iostream(cin/cout/cerr),fstream(ifstream/ofstream/fstream),sstream(istringstream/ostringstream/stringstream)。自己刚开始时还犯了#include 这样的错误。

2. 文件的输入与输出
     文件的输入与输出主要依赖fstream/istream/ostream三种类,配合使用打开的模式从而实现文件的读取和写入。主要步骤如下:

创建一个文件流对象,绑定文件名,打开该文件:
     ifstream ifile("test");     //定义对象时使用构造函数初始化
     ofstream ofile;
     ofile.open("test", ofstream::out | ofstream::app);
     string name("test");
     fstream file;
     file.open(name.c_str(), fstream::in | fstream::out); //由于兼容性考虑,open函数只能接受C格式字符串,因此必须将string提取为C格式字符串,by c_str()方法

检查文件是否成功打开绑定:
     if (!file)
     {
          cerr << "Unable to open file: "
                 << file << endl;
          return -1;
     }

向文件对象写入/读取:
     ofile << "Hello, World!"<< endl;
     getline(ifile, string);    //从ifile读取一行字符串写入string中

关闭文件,清空状态:
    file.close();
    file.clear();
注意!单纯close文件对象不会清空记录的条件状态,因此如果重复使用同一个文件对象务必调用clear函数将其状态清空刷新!

3. 打开模式的几个问题
     与ifstream流对象关联的文件将默认以ifstream::in模式打开;与ofstream流对象关联的文件对象则默认以ofstream::out和ofstream::trunk模式打开;与fstream流对象关联的文件对象则默认以fstream::in | fstream::out模式打开。一般来说,几种文件打开模式可以自由组合,但是有一些简单的规则,比如in和app是不能搭配的,同样in和trunk(清空文件内容)也是不能搭配的,可用的有效搭配有:
     out;   out | app;   out | trunk
     in;   in | out;   in | out | trunk
4. 一个例子程序:

点击(此处)折叠或打开

  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;

  5. int main()
  6. {
  7.     //test ofstream
  8.     ofstream o_file;
  9.     o_file.open("test-file.txt",ofstream::out);
  10.     if (!o_file)
  11.     {
  12.         cerr << "Unable to open o_file !"
  13.              << o_file <<endl;
  14.         system("pause");
  15.         return -1;
  16.         }
  17.     o_file << "This is a ofstream test!" <<endl;
  18.     o_file.close();
  19.     o_file.clear();
  20.     
  21.     //test ifstream
  22.     string str_fi;
  23.     ifstream i_file;
  24.     i_file.open("test-file.txt", ifstream::in);
  25.     if (!i_file)
  26.     {
  27.          cerr << "Unable to open i_file !"
  28.               << i_file <<endl;
  29.          system("pause");
  30.          return -2;
  31.          }
  32.     getline(i_file, str_fi);
  33.     cout << "string in file is : "
  34.          << str_fi << endl;
  35.     //test
  36.     //string str_tst;
  37.     //i_file >> str_tst;
  38.     //cout << "str_tst: "
  39.     // << str_tst <<endl;
  40.     i_file.close();
  41.     i_file.clear();
  42.     
  43.     //test fstream
  44.     fstream file;
  45.     file.open("test-file.txt", fstream::out |fstream::app);
  46.     if (!file)
  47.     {
  48.          cerr << "Unable to open file !"
  49.               << file <<endl;
  50.          system("pause");
  51.          return -3;
  52.          }
  53.     file << "This is a fstream test!"
  54.          <<endl;
  55.     file.close();
  56.     file.clear();
  57.     
  58.     system("pause");
  59.     return 0;
  60.     
  61.     
  62.     
  63. }


阅读(4910) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~