read 32 bit integer from binary file in c++

0

i'm trying to read a 32 bit integer from a binary file in c++. My goal is to obtain something similar to the c# BinaryReader.ReadInt32() method. As specified here, this c# method reads a 4 bytes signed integer. I have verified that my input file is little-endian, and the first 4 bytes read by the c# method are 0x0000001E (aka 30), which is correct, i double checked it using a binary editor.

I've already tried to follow the answer here but my code doesn't seem to work:

std::fstream _inputStream(fname.c_str(), std::ios_base::binary | std::ios_base::in | std::ios::ate);

if (!_inputStream.is_open()) {
    std::cout << "Error opening the file\n" << fname << "\n" << std::endl;      
}

uint32_t a;
_inputStream.read(reinterpret_cast<char*>(&a), sizeof(a));

I keep getting a = 0xCCCCCCCC, what am i missing?

thanks in advance

c#
c++
fstream
asked on Stack Overflow Nov 28, 2019 by rekotc

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0