Chinaunix首页 | 论坛 | 博客
  • 博客访问: 102128
  • 博文数量: 30
  • 博客积分: 305
  • 博客等级: 二等列兵
  • 技术积分: 320
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-09 12:31
文章分类

全部博文(30)

文章存档

2014年(3)

2013年(16)

2012年(11)

我的朋友

分类: C/C++

2012-11-28 14:38:05

1. filebuf::open(const char*, mode); 第一个参数表示文件,第二个参数对应打开方式,如ios::in输入
2. getline(istream &, string &, char del); 第一个参数打开的流,第二个参数保存读入的内容,第三个参数字段的分割副,默认是 '\n'
3. string::find(); 查找某一个字符在字符串中的位置

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. /** 读文件
  6.  */
  7. int main(int argc,char **argv)
  8. {
  9.     filebuf fb;
  10.     string filename = "test.txt";
  11.     if(fb.open(filename.c_str(),ios::in) == NULL)
  12.     {
  13.         cout << "error" << endl;
  14.     }
  15.     istream is(&fb);
  16.     string input;
  17.     while(getline(is,input,'\n'))
  18.     {
  19.         int pos1 = string::npos;
  20.         pos1 = input.find("\t");
  21.         if(pos1 != string::npos)
  22.         {
  23.             cout << input.substr(pos1+1) << endl;
  24.         }
  25.         else
  26.         {
  27.             cout << "eror";
  28.             break;
  29.         }
  30.     }
  31.     fb.close();
  32.     return 0;
  33. }

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