Ricky
Ricky

Reputation: 41

ifstream::read keeps returning incorrect value

I am currently working my way through teaching myself how to work with files in c++, and I am having a good bit of difficulty extracting binary information from files.

My code:

std::string targetFile = "simplehashingfile.txt";
const char* filename = targetFile.c_str();
std::ifstream file;

file.open( filename, std::ios::binary | std::ios::in );
file.seekg(0, std::ios::end);  //  go to end of file
std::streamsize size = file.tellg();  //  get size of file

std::vector<char> buffer(size);  //  create vector of file size bytes

file.read(buffer.data(), size);  //  read file into buffer vector
int totalread = file.gcount();

//  Check that data was read
std::cout<<"total read: " << totalread << std::endl;


//  check buffer:  
std::cout<<"from buffer vector: "<<std::endl;
for (int i=0; i<size; i++){
    std::cout << buffer[i] << std::endl;
}
std::cout<<"\n\n";

The "simplehashingfile.txt" file only contains 50 bytes of normal text. The size is correctly determined to be 50 bytes, but gcount returns 0 chars read, and the buffer output is (understandably from the gcount) a 50 line list of nothing.

For the life of me I cannot figure out where I went wrong! I made this test code earlier:

//  Writing binary to file
std::ofstream ofile;
ofile.open("testbinary", std::ios::out | std::ios::binary);

uint32_t bytes4 = 0x7FFFFFFF;  //  max 32-bit value
uint32_t bytes8 = 0x12345678;  //  some 32-bit value


ofile.write( (char*)&bytes4 , 4 );
ofile.write( (char*)&bytes8, 4 );

ofile.close();



//  Reading from file
std::ifstream ifile;
ifile.open("testbinary", std::ios::out | std::ios::binary);

uint32_t reading;  //  variable to read data 
uint32_t reading2;

ifile.read( (char*)&reading, 4 );
ifile.read( (char*)&reading2, 4 );

std::cout << "The file contains:  " << std::hex << reading << std::endl;
std::cout<<"next 4 bytes:  "<< std::hex << reading2 << std::endl;

And that test code wrote and read perfectly. Any idea what I am doing wrong? Thank you to anyone who can point me in the right direction!

Upvotes: 1

Views: 1684

Answers (3)

Wayne Tanner
Wayne Tanner

Reputation: 1356

I think the problem is that you do a seek to the end to get the file size, but don't seek back to the beginning before trying to read the file.

Upvotes: 0

Christophe
Christophe

Reputation: 73376

It would be worth to return to the begin of the file, before trying to read:

file.seekg(0, std::ios::beg)

Upvotes: 1

NathanOliver
NathanOliver

Reputation: 180500

You never reset the file back to the beginning when you read from it

std::streamsize size = file.tellg(); //<- goes to the end of the file
std::vector<char> buffer(size);  //  create vector of file size bytes

file.read(buffer.data(), size);  //<- now we read from the end of the file which will read nothing
int totalread = file.gcount();

You need to call seekg() again and reset the file pointer back to the beginning. To do that use

fille.seekg(0, std::ios::beg);

before

file.read(buffer.data(), size);

Upvotes: 3

Related Questions