[英]ifstream::read doesn't tell how many bytes it really reads?
I'm using ifstream::read
to read a file, 我正在使用
ifstream::read
来读取文件,
ifstream ifs("a.txt");
char buf[1024];
ifs.read(buf, 1024);
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
? 但是a.txt的大小可能小于
1000 bytes
,所以我怎么知道从ifs
读取了多少字节?
You can get the amount of characters extracted by the last operation with std::ifstream::gcount
: 您可以使用
std::ifstream::gcount
上一次操作提取的字符std::ifstream::gcount
:
ifstream ifs("a.txt");
char buf[1024];
ifs.read(buf, 1024);
size_t extracted = ifs.gcount();
or 要么
ifstream ifs("a.txt");
char buf[1024];
size_t extracted = ifs.read(buf, 1024).gcount();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.