ifstream :: read不能告诉它真正读取多少字节? [英] ifstream::read doesn't tell how many bytes it really reads?
本文介绍了ifstream :: read不能告诉它真正读取多少字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 ifstream :: read
读取档案,
ifstream ifs("a.txt");
char buf[1024];
ifs.read(buf, 1024);
但是a.txt的大小可能小于 1000字节
,所以我应该知道从 ifs
?中读取了多少字节?
But a.txt's size might be less than 1000 bytes
, so how am I supposed to know how many bytes have been read from ifs
?
推荐答案
您可以使用 <$ c,获取上次操作提取的字符数量$ c> std :: ifstream :: gcount :
You can get the amount of characters extracted by the last operation with std::ifstream::gcount
:
ifstream ifs("a.txt");
char buf[1024];
ifs.read(buf, 1024);
size_t extracted = ifs.gcount();
或
ifstream ifs("a.txt");
char buf[1024];
size_t extracted = ifs.read(buf, 1024).gcount();
因为 read(...)
返回 * this
。
这篇关于ifstream :: read不能告诉它真正读取多少字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文