std :: ifstream :: read()有什么问题? [英] What's wrong with std::ifstream::read()?

查看:109
本文介绍了std :: ifstream :: read()有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道至少有一个人认为std :: ifstream :: read()和

std :: ofstream :: write()是错误。他们似乎完成了我想要的工作

完成。他们怎么了。这是我目前使用std :: ifstream :: read()作为测试的代码
。我是这样获取文件的方式有什么不对吗?


#include< vector>

#包括< iomanip>

#include< fstream>

#include< iostream>


模板< typename Iterator>

std :: ostream& printHexLine(Iterator start,Iterator stop,std :: ostream& out)

{

while(start< stop)out

<< ; std :: setw(2)

<<(static_cast< unsigned int>(static_cast< unsigned char>(* start ++)))<<"

" ;;

退出;

}


模板< typename容器>

的std :: ostream的和放; print(const Container& data,std :: ostream& out){

typedef typename Container :: const_iterator c_itr;


std :: ostream hexout(out .rdbuf());

hexout.setf(std :: ios :: hex,std :: ios :: basefield);

hexout.fill(''0 '');


c_itr来自(data.begin());

c_itr dataEnd(来自+ data.size());

c_itr end(dataEnd - (data.size()%16));


for(c_itr start = from; start< end; start + = 16)printHexLine (开始,

start + 16,hexout)<<" \ n" ;;


printHexLine(end,dataEnd,hexout)< <" \ n" ;;

退出;

}


int main(int argc,char * argv []){

std :: string filename(" fileio");

std :: ifstream文件(filename.c_str(),std :: ios :: in | std :: ios :: binary

std :: ios :: ate);

std :: vector< char> vbuf(file.tellg());

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

file.read(& vbuf [0],vbuf.size());

print(vbuf,std :: cout);

返回0 ;

}


-

如果我们的假设是关于任何事情而不是关于某一个或多个

特定的东西,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell

I know of a least one person who believes std::ifstream::read() and
std::ofstream::write() are "mistakes". They seem to do the job I want
done. What''s wrong with them. This is the code I currently have as a test
for using std::ifstream::read(). Is there anything wrong with the way I''m
getting the file?

#include <vector>
#include <iomanip>
#include <fstream>
#include <iostream>

template<typename Iterator>
std::ostream& printHexLine(Iterator start, Iterator stop, std::ostream& out)
{
while(start<stop) out
<<std::setw(2)
<<(static_cast<unsigned int>(static_cast<unsigned char>(*start++)))<<"
";
return out;
}

template<typename Container>
std::ostream& print(const Container& data, std::ostream& out) {
typedef typename Container::const_iterator c_itr;

std::ostream hexout(out.rdbuf());
hexout.setf(std::ios::hex, std::ios::basefield);
hexout.fill(''0'');

c_itr from (data.begin());
c_itr dataEnd (from + data.size());
c_itr end (dataEnd - (data.size()%16));

for(c_itr start = from; start < end; start += 16) printHexLine(start,
start + 16, hexout)<<"\n";

printHexLine(end, dataEnd, hexout)<<"\n";
return out;
}

int main (int argc, char* argv[]) {
std::string filename("fileio");
std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary
std::ios::ate);
std::vector<char>vbuf(file.tellg());
file.seekg(0, std::ios::beg);
file.read(&vbuf[0], vbuf.size());
print(vbuf, std::cout);
return 0;
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell

推荐答案



Steven T. Hatton写道:

Steven T. Hatton wrote:
我知道至少有一个人认为std :: ifstream :: read()和
std :: ofstream :: write()是错误。他们似乎完成了我想做的工作。他们怎么了。这是我目前使用std :: ifstream :: read()作为测试的代码。我收到文件的方式有什么问题吗?


[]

std :: vector< char> vbuf(file.tellg());
file.seekg(0,std: :ios :: beg);
file.read(& vbuf [0],vbuf.size());
I know of a least one person who believes std::ifstream::read() and
std::ofstream::write() are "mistakes". They seem to do the job I want
done. What''s wrong with them. This is the code I currently have as a test
for using std::ifstream::read(). Is there anything wrong with the way I''m
getting the file?
[]
std::vector<char>vbuf(file.tellg());
file.seekg(0, std::ios::beg);
file.read(&vbuf[0], vbuf.size());




你不要需要在这里阅读一个简单的:


std :: vector< char> vbuf((istreambuf_iterator< char>(f ile)),

(istreambuf_iterator< char>() ));


就够了。虽然您可能会认为您的代码不涉及

向量重新分配。



You don''t need read here. A simple:

std::vector<char>vbuf((istreambuf_iterator<char>(f ile)),
(istreambuf_iterator<char>()));

Would suffice. Although you might argue that your code does not involve
vector reallocations.


Maxim Yegorushkin写道:
Maxim Yegorushkin wrote:

Steven T. Hatton写道:

Steven T. Hatton wrote:
我知道至少有一个人相信std :: ifstream :: read()和
std :: ofstream :: write()是错误。他们似乎完成了我想做的工作。他们怎么了。这是我目前使用std :: ifstream :: read()作为
测试的代码。
我收到档案的方式有什么问题吗?
I know of a least one person who believes std::ifstream::read() and
std::ofstream::write() are "mistakes". They seem to do the job I want
done. What''s wrong with them. This is the code I currently have as a
test
for using std::ifstream::read(). Is there anything wrong with the way
I''m getting the file?






[]

std :: vector< char> vbuf(file.tellg());
file.seekg(0,std :: ios :: beg);
file.read(& vbuf [0],vbuf.size() );
std::vector<char>vbuf(file.tellg());
file.seekg(0, std::ios::beg);
file.read(&vbuf[0], vbuf.size());



你不需要在这里阅读。一个简单的:

std :: vector< char> vbuf((istreambuf_iterator< char>(f ile)),
(istreambuf_iterator< char>()));

就够了。虽然您可能会认为您的代码不涉及矢量重新分配。



You don''t need read here. A simple:

std::vector<char>vbuf((istreambuf_iterator<char>(f ile)),
(istreambuf_iterator<char>()));

Would suffice. Although you might argue that your code does not involve
vector reallocations.




我可以在向量中保留空间,但仍然使用迭代器。我不知道

用std :: ios_base :: ate打开的确切含义是什么。

强制操作系统尝试将整个文件加载到内存中吗?我知道

语言并没有指定,它可能与操作系统有关。在我看来

迭代器可能做了很多工作,真的不需要做完b
。我真正想做的是从ifstream窃取缓冲区

而不是复制它。


我不知道任何性能评估比较不同的

阅读文件的技巧,但我确实知道我做过的一项工作,我们拥有世界上最大的人力资源记录系统,全部采用扫描的形式

图像。在这种情况下浪费复制并不是一个好主意。


-

如果我们的假设是关于任何事情而不是关于某一个或多个那么多

特定的东西,然后我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell




Steven T. Hatton写道:


[]

Steven T. Hatton wrote:

[]
我可以在向量中保留空间,仍然使用迭代器。我不知道用std :: ios_base :: ate打开的确切含义是什么。是否强制操作系统尝试将整个文件加载到内存中?我知道
语言没有指定,它可能与操作系统有关。在我看来,迭代器可能做了很多工作,真的不需要完成。我真正想做的是从ifstream中窃取缓冲区而不是复制它。
I can reserve space in the vector, and still use the iterator. I don''t know
what the exact implications of opening with std::ios_base::ate are. Does
that force the OS to try loading the entire file into memory? I know the
language doesn''t specify, and it may well be OS dependent. It seemed to me
the iterator is probably doing a lot of work that really didn''t need to be
done. What I really want to do is steal the buffer from the ifstream
rather than copy it.




我使用内存映射文件。 POSIX / win32实现是非常简单的





I use memory mapped files for that. A POSIX/win32 implementation is
trivial.


这篇关于std :: ifstream :: read()有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆