std::ifstream 读取文件

简介:

1 头文件

#include <iostream>

#include <fstream>

#include <string>


2 读取一行

void UsingifstreamReadLineMethod()

{

char szBuf[256] = { 0 };

std::ifstream  fileHandle("E:/thriftserver/output/facealarmnew.txt");

fileHandle.getline(szBuf, 100);

size_t nLen = strlen(szBuf);

}


3 读取整个文本

void UsingifstreamReadMethod1()

{

std::ifstream fileHandle;

int nFileLen = 0;

fileHandle.open("E:/thriftserver/output/facealarmnew.txt");

fileHandle.seekg(0, std::ios::end);

nFileLen = fileHandle.tellg();

fileHandle.seekg(0, std::ios::beg);

char szFileBuf[4096] = { 0 };

fileHandle.read(szFileBuf, nFileLen);

std::cout << nFileLen << std::endl;

fileHandle.close();

}


void UsingifStreamReadMethod2()

{

std::ifstream fileHandle;

fileHandle.open("E:/thriftserver/output/facealarmnew.txt");

std::string strFileBuf;

fileHandle >> strFileBuf;

std::cout << strFileBuf.length() << std::endl;

}


上述的两个方法,都尝试读取整个文本的数据,但是第二个方法在读取Json格式的数据情况下,会出现读取不完整的情况,文本长度是2876,但是第二个方法读取到的字符串长度是871,所以在使用的过程中,一定要谨慎小心,目前怀疑是在读取到空字符的时候,就直接返回了,导致数据读取出错


4直接将ifstream文件句柄传递给Jsoncpp解析器,进行文本的解析

void UsingifstreamReadJson()

{

std::ifstream fileHandle;

fileHandle.open("E:/thriftserver/output/facealarmnew.txt");


Json::Reader reader;

Json::Value root;

if (NULL == reader.parse(fileHandle, root))

{

fileHandle.close();

return;

}


fileHandle.close();

std::string strName = root["name"].asString();

}


参考文章备忘


c++中一次读取整个文件的内容的方法:


读取至char*的情况

std::ifstream t;  

int length;  

t.open("file.txt");      // open input file  

t.seekg(0, std::ios::end);    // go to the end  

length = t.tellg();           // report location (this is the length)  

t.seekg(0, std::ios::beg);    // go back to the beginning  

buffer = new char[length];    // allocate memory for a buffer of appropriate dimension  

t.read(buffer, length);       // read the whole file into the buffer  

t.close();                    // close file handle  

  

// ... do stuff with buffer here ...  

读取至std::string的情况:

第一种方法:


#include <string>  

#include <fstream>  

#include <streambuf>  

  

std::ifstream t("file.txt");  

std::string str((std::istreambuf_iterator<char>(t)),  

                 std::istreambuf_iterator<char>()); 

第二种方法:


#include <string>  

#include <fstream>  

#include <sstream>  

std::ifstream t("file.txt");  

std::stringstream buffer;  

buffer << t.rdbuf();  

std::string contents(buffer.str());

reference


http://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring


摘自:http://www.cnblogs.com/kex1n/p/4028428.html




     本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1570348,如需转载请自行联系原作者

相关文章
C++中使用 ofstream ifstream 写入读取文件
C++中使用 ofstream ifstream 写入读取文件
|
缓存 关系型数据库 iOS开发
fstream,ifstream,ofstream 详解与用法
fstream,istream,ofstream 三个类之间的继承关系fstream :(fstream继承自istream和ofstream)1.typedef basic_fstream fstream;// 可以看出fstream就是basic_fstream2.
1597 0
|
缓存 C++ iOS开发
转载:ofstream和ifstream详细用法
ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的文件I/O,stream这个类有两个重要的运算符: 1、插入器(
949 0
|
C++ iOS开发
转载:fstream和ifstream详细用法
文件 I/O 在C++中比烤蛋糕简单多了。在这篇文章里,我会详细解释ASCII和二进制文件的输入输出的每个细节,值得注意的是,所有这些都是用C++完成的。   一、ASCII 输出   为了使用下面的方法, 你必须包含头文件(译者注:在标准C++中,已经使用取代< fstream.h>,所有的C++标准头文件都是无后缀的。
977 0
|
10月前
|
存储 缓存 程序员
C++ 文件读写:探索 ofstream, ifstream 和 fstream 的奥秘
C++ 文件读写:探索 ofstream, ifstream 和 fstream 的奥秘
736 0
|
C++
std::ofstream failed
今天在输出数据到文件时,用到C++的std::ofstream,结果文件就是没有输出成功,示例程序如下所示: #include #include #include #include void Output(const std::string &theFileName){    std::ofstream os(theFileName.
1140 0
|
C++
【C++ 语言】文件操作 ( fopen | fprintf | fscanf | fgets | fputc | fgetc | ofstream | ifstream )(二)
【C++ 语言】文件操作 ( fopen | fprintf | fscanf | fgets | fputc | fgetc | ofstream | ifstream )(二·)
368 0
|
存储 C语言 C++
【C++ 语言】文件操作 ( fopen | fprintf | fscanf | fgets | fputc | fgetc | ofstream | ifstream )(一)
【C++ 语言】文件操作 ( fopen | fprintf | fscanf | fgets | fputc | fgetc | ofstream | ifstream )(一)
330 0
|
C++ C语言
std::fstream 中文路径
std::fstream 中文路径 eryar@163.com 用C++来开发管道出图程序IsoAlgo时,当PCF文件名中包含中文时,读取文件会失败。将下面数据存成一个简单文件:中文.txt  放到目标目录中来测试: Figure 1 包含中文的文件 简单测试程序代码如下所示: #i...
1757 0