C++ ifstream,字节读取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1937408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:38:57  来源:igfitidea点击:

ifstream, bytes read?

c++

提问by user230821

How do you get how many bytes were read with the ifstream::read function?

你如何获得使用 ifstream::read 函数读取的字节数?

Tell is saying the file is 10 bytes and windows says it is 10 bytes too but there are only 8 bytes in the file so when I read it, it is only reading the 8 bytes so I end up with too large of a buffer.

Tell 说文件是 10 个字节,Windows 说它也是 10 个字节,但文件中只有 8 个字节,所以当我读取它时,它只读取 8 个字节,所以我最终得到了太大的缓冲区。

回答by Alex B

You can find out by calling gcount()on a stream immediately after you read.

您可以通过gcount()在阅读后立即调用流来查找。

ifs.read(buf, sizeof buf);
std::streamsize bytes = ifs.gcount();

回答by AraK

There is a function called readsome(...)that does what you want:

有一个名为的函数readsome(...)可以执行您想要的操作:

streamsize readsome ( char* s, streamsize n );

Return Value The number of characters extracted.

返回值 提取的字符数。