c以行为单位读取文件.docVIP

  • 16
  • 0
  • 约1.1千字
  • 约 2页
  • 2017-06-10 发布于北京
  • 举报
c++以行为单位读取文件 张小强 2012年11月 qq:1532589421 有的时候处理文本类型文件需要一行为单位进行处理,方法有很多种,我现在给大家提供一个应用stl库处理,得到结果保存在vector中。我所提供的代码,直接拷贝下来写入头文件和源文件中,再include进您自己的工程就可以了,绝对方便使用。 头文件: #ifndef READFILEBYROW_H_H_H #define READFILEBYROW_H_H_H #include stdio.h #include vector #include string std::vectorstd::string ReadFileByRow(const std::string filePath); #endif 源文件 #include stdafx.h #include ReadFileByRow.h std::vectorstd::string ReadFileByRow(const std::string filePath) { FILE *pFile; if((pFile=fopen(filePath.c_str(),r))!=NULL) { char *pBuf,*p; fseek(pFile,0,SEEK_END); int len=ftell(pFile); pBuf=new char[len]; rewind(pFile); memset(pBuf,0,len); std::vectorstd::string temp; while (1) { p=fgets(pBuf,len,pFile); std::string str(pBuf); temp.push_back(str); if (!p) { break; } } fclose(pFile); delete pBuf; return temp; } else { printf(文件无法读取!); fclose(pFile); std::vectorstd::string temp; return temp; } } 使用指南: 函数接口std::vectorstd::string ReadFileByRow(const std::string filePath); 使用时传递string类型的文件路径,返回string的vector

文档评论(0)

1亿VIP精品文档

相关文档