[C++]逐行读取文件与分割字符串方法

遇上文本处理的话,基本上都是先读取文件,再逐行读取文本,再进行文本分割。

1、读取文本

使用头文件#include <fstream>,包含ifstream-输入流,ofstream-输出流,iostream-输入输出流三种类型的文件流。

ifstream iFile;
int a;
iFile.open(filename);//ifstream默认以读方式打开
//do your job
iFile>>a;
iFile.close();

2、逐行读取

char buf[300000];
while (!iFile.eof()){// find the end of the file
    iFile.getline(buf,300000);}

3、分割字符串

方法1:使用strtok函数

strtok会在分割的位置添加一个\0,返回每一个分割部分的头指针。所以它返回的是buf上的地址,当buf失效了,它返回的指针也失效了。其次,因为它需要改变原字符串,所以buf不能传入const char*类型

const char* d=" *"; //token 分隔符,支持多个分隔符,如这里空格和*
char* p=strtok(buf,d);
while(p){
    cout<<p<<" ";
    p=strtok(NULL,d);//循环过程要把buf赋为NULL
    }

方法2:使用STL find和substr函数

涉及到string类的两个函数find和substr:
find函数
原型:size_t find ( const string& str, size_t pos = 0 ) const;
功能:查找子字符串第一次出现的位置
参数说明:str为子字符串,pos为初始查找位置。
返回值:找到的话返回第一次出现的位置,否则返回string::npos

substr函数
原型:string substr ( size_t pos = 0, size_t n = npos ) const;
功能:截取从pos开始到n结束这段字符串。
参数说明:pos为起始位置(默认为0),n为结束位置(默认为npos)
返回值:子字符串

//字符串分割函数
 std::vector<std::string> split(std::string str,std::string pattern)
 {
     std::string::size_type pos;
     std::vector<std::string> result;
     str+=pattern;//扩展字符串以方便操作
     int size=str.size();
 
     for(int i=0; i<size; i++)
     {
         pos=str.find(pattern,i);
         if(pos<size)
         {
             std::string s=str.substr(i,pos-i);
             result.push_back(s);
             i=pos+pattern.size()-1;
         }
     }
     return result;
 }

 C语言中的read()函数(linux管道中也是使用read):

ssize_t read(int fd,void *buf,int count),从文件描述符fd中读取count长度,存入buf中,返回实际读取的大小。如返回值少于count则文件已经读完,如等于则代表buf不足以放下整个文件。

如文件没读取完成,文件的指针会自动记录当前读取的地方,下次调用read()从未读处开始。

  int fd;
    char buf[BUF_SIZE];
    fd=open("f:/test.txt",0);
    if(-1==fd)
        return -1;
    int n=read(fd,buf,2);
    if(-1==n || 0==n){
        return -1;
        close(fd);
    }

 

posted @ 2013-04-10 10:03  iyjhabc  阅读(4766)  评论(0编辑  收藏  举报