- 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
您可能关注的文档
- CAG的中西医结合治疗.doc
- CALLING动漫歌曲歌词中日互译有罗马音注释.doc
- candece快捷键说明.doc
- CAN总线传送到数据是基于消息而不是地址的.docx
- CASS内业补充程序命令.doc
- Cathy的静心话语:接纳自己.doc
- CATIA在机械制图教学中的应用.doc
- CC(含数据结构)知识点.doc
- CCU是医院中病情重.doc
- CC结构体字节对齐详解.docx
- 广东省广州省实验中学教育集团2025-2026学年八年级上学期期中考试物理试题(解析版).docx
- 广东省广州大学附属中学2025-2026学年八年级上学期奥班期中物理试题(解析版).docx
- 广东省广州市第八十六中学2025-2026学年八年级上学期期中物理试题(含答案).docx
- 广东省广州市第八十九中学2025-2026学年八年级上学期期中考试物理试题(解析版).docx
- 广东省广州市第二中学2025-2026学年八年级上学期期中考试物理试题(含答案).docx
- 广东省广州市第八十六中学2025-2026学年八年级上学期期中物理试题(解析版).docx
- 广东省广州市第八十九中学2025-2026学年八年级上学期期中考试物理试题(含答案).docx
- 广东省广州市第二中学2025-2026学年八年级上学期期中考试物理试题(解析版).docx
- 2026《中国人寿上海分公司营销员培训体系优化研究》18000字.docx
- 《生物探究性实验教学》中小学教师资格模拟试题.docx
原创力文档

文档评论(0)