简体   繁体   中英

ifstream doesn't read to buffer

In the following code the read method doesn't seem to fill the given buffer:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM