文件长度... [英] file length...

查看:75
本文介绍了文件长度...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试将二进制文件读入缓冲区:


std :: ifstream ifs(fileName,

std :: ios :: in | std :: ios :: binary);

if(!ifs)

return;

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

int len = ifs.tellg();

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

char * buf = new char [len];

memcpy(buf,ifs.rdbuf(),len);


但是,这不能按预期工作,因为

buf不包含原始文件所包含的内容......


我不想从

文件中逐个字符地阅读,上面的内容不起作用。那么什么是

这样做的正确方法简单东西?


谢谢,

史蒂夫

解决方案

Steve写道:< blockquote class =post_quotes>
我正在尝试将二进制文件读入缓冲区:


请参阅:

< a rel =nofollowhref =http://www.cplusplus.com/ref/iostream/istream/read.htmltarget =_ blank> http://www.cplusplus.com/ref/iostream/istream /read.html

std :: ifstream ifs(fileName,
std :: ios :: in | std :: ios :: binary);
if (!ifs)
返回;
ifs.seekg(0,std :: ios :: end);
int len = ifs.tellg();
ifs.seekg( 0,std :: ios :: beg);
char * buf = new char [len];
memcpy(buf,ifs.rdbuf(),len);

但是,这并没有按预期工作,因为
buf不包含原始文件所包含的内容...

我不想从
文件中逐字逐句地阅读以上不起作用。那么这种简单的正确方法是什么呢?东西?

谢谢,
史蒂夫



史蒂夫写道:


我正在尝试将二进制文件读入缓冲区:

std :: ifstream ifs(fileName,
std :: ios: :in | std :: ios :: binary);
if(!ifs)
return;
ifs.seekg(0,std :: ios :: end);
int len = ifs.tellg();
ifs.seekg(0,std :: ios :: beg);
char * buf = new char [len];
memcpy(buf, ifs.rdbuf(),len);




ifs.rdbuf()返回指向类basic_filebuf实例的指针。复制

将这个对象组成的字节组成一个char数组,无论如何都不会产生
。但你可以使用它的成员函数sgetn来获取数据。


Steve写道:

ifs.seekg(0,std: :ios :: end);
int len = ifs.tellg();
ifs.seekg(0,std :: ios :: beg);
char * buf = new char [ LEN];


这是一个非常危险的方法:实际上没有

便携式方法来确定

文件中的字符数除了计算它们,例如像这样:


/ ** / std :: streamsize len = std :: distance(

/ ** / std :: istreambuf_iterator< char>( ifs),

/ ** / std :: istreambuf_iterator< char>());

memcpy(buf,ifs.rdbuf(),len);



这不读取文件,它复制了

的内容。不要这样做!用C ++读取文件的最有效和可靠的便携式方法可能就像

这样:


/ ** / std :: istringstream out;

/ ** / out<< ifs.rdbuf();

/ ** / std :: string buf = out.str();


我个人首选的符号是:


/ ** / std :: vector< char> buf;

/ ** / std :: copy(std :: istreambuf_iterator< char>(ifs),

/ ** / std :: istreambuf_iterator< char>( ),

/ ** / std :: back_inserter(buf));


....但这通常比其他的慢得多/>
方法虽然实际上可以更快。 :-(

-

< mailto:di *********** @ yahoo.com>< http:// www。 dietmar-kuehl.de/>

< http://www.contendix.com> - 软件开发与咨询


Hi,
I''m trying to read a binary file into a buffer:

std::ifstream ifs(fileName,
std::ios::in|std::ios::binary);
if (!ifs)
return;
ifs.seekg(0,std::ios::end);
int len = ifs.tellg();
ifs.seekg(0,std::ios::beg);
char* buf = new char[len];
memcpy(buf,ifs.rdbuf(),len);

However, this is not working as expected since
buf contains not what the original file contains...

I do not want to read character-by-character from
file and the above does not work. So what is the
correct way of doing this "simple" stuff?

Thanks,
Steve

解决方案

Steve wrote:

Hi,
I''m trying to read a binary file into a buffer:
See:

http://www.cplusplus.com/ref/iostream/istream/read.html

std::ifstream ifs(fileName,
std::ios::in|std::ios::binary);
if (!ifs)
return;
ifs.seekg(0,std::ios::end);
int len = ifs.tellg();
ifs.seekg(0,std::ios::beg);
char* buf = new char[len];
memcpy(buf,ifs.rdbuf(),len);

However, this is not working as expected since
buf contains not what the original file contains...

I do not want to read character-by-character from
file and the above does not work. So what is the
correct way of doing this "simple" stuff?

Thanks,
Steve



Steve wrote:

Hi,
I''m trying to read a binary file into a buffer:

std::ifstream ifs(fileName,
std::ios::in|std::ios::binary);
if (!ifs)
return;
ifs.seekg(0,std::ios::end);
int len = ifs.tellg();
ifs.seekg(0,std::ios::beg);
char* buf = new char[len];
memcpy(buf,ifs.rdbuf(),len);



ifs.rdbuf() returns a pointer to an instance of class basic_filebuf. Copying
the bytes that this objects is made of into an array of char doesn''t make
any sense. But you can use its member function sgetn to get data.


Steve wrote:

ifs.seekg(0,std::ios::end);
int len = ifs.tellg();
ifs.seekg(0,std::ios::beg);
char* buf = new char[len];
This is a pretty dangerous approach: there is actually no
portable approach to determine the number of characters in a
file except counting them, e.g. like this:

/**/ std::streamsize len = std::distance(
/**/ std::istreambuf_iterator<char>(ifs),
/**/ std::istreambuf_iterator<char>());
memcpy(buf,ifs.rdbuf(),len);



This does not read the files, it copies the contents of
some struct. Don''t do it! Probably the most efficient and
portable approach to reading a file in C++ is something like
this:

/**/ std::istringstream out;
/**/ out << ifs.rdbuf();
/**/ std::string buf = out.str();

My personally preferred notation would be this:

/**/ std::vector<char> buf;
/**/ std::copy(std::istreambuf_iterator<char>(ifs),
/**/ std::istreambuf_iterator<char>(),
/**/ std::back_inserter(buf));

.... but this is typically considerably slower than the other
approach although it could actually be made faster. :-(
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting


这篇关于文件长度...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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