[英]ifstream doesn't read to buffer
In the following code the read method doesn't seem to fill the given buffer: 在以下代码中,read方法似乎没有填充给定的缓冲区:
ifstream pkcs7_file(file_name, std::ios::binary);
if ( pkcs7_file.fail() )
{
std::cout << "File failed before reading!\n";
}
pkcs7_file.seekg(0, pkcs7_file.end);
size_t len = pkcs7_file.tellg();
char * buffer = new char[len];
pkcs7_file.read(buffer, len);
pkcs7_file.close();
When debugging with VS 2012 and printing, the Len variable is as expected (and not zero) but the buffer doesn't change after the read function - it remains with the same value from before the read. 使用VS 2012进行调试和打印时,Len变量符合预期(而不是零),但在读取函数后缓冲区不会更改-它与读取之前的值保持不变。
What am I doing wrong? 我究竟做错了什么?
You seek to end-of-file, and then try to read. 您试图找到文件结尾, 然后尝试阅读。 Of course it fails - the file is positioned at EOF, there's no data to read.
当然会失败-文件位于EOF,没有要读取的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.