设为首页 加入收藏

TOP

c++ 按行读取 (getline)
2012-11-30 12:27:23 来源: 作者: 【 】 浏览:351
Tags:  读取 getline

    1. filebuf::open(const char*, mode); 第一个参数表示文件,第二个参数对应打开方式,如ios::in输入

    2. getline(istream &, string &, char del); 第一个参数打开的流,第二个参数保存读入的内容,第三个参数字段的分割副,默认是 '\n'

    3. string::find(); 查找某一个字符在字符串中的位置

    #include <iostream>

    #include <string>

    #include <fstream>

    using namespace std;

    /** 读文件

    */

    int main(int argc,char **argv)

    {

    filebuf fb;

    string filename = “test.txt”;

    if(fb.open(filename.c_str(),ios::in) == NULL)

    {

    cout << “error” << endl;

    }

    istream is(&fb);

    string input;

    while(getline(is,input,'\n'))

    {

    int pos1 = string::npos;

    pos1 = input.find(“\t”);

    if(pos1 != string::npos)

    {

    cout << input.substr(pos1+1) << endl;

    }

    else

    {

    cout << “eror”;

    break;

    }

    }

    fb.close();

    return 0;

    }

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇一个C++中智能指针的设计 下一篇xml解析生成pdf,word文档

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: