my C++ knowledge is very limited so my apologies beforehand as it may be a very simple question but I haven't been able to find a solution.
I have a binary file that I'm trying to read. The code attempting to read the binary file is shown below:
string readFile2(const string &fileName)
{
cout << "B1 \n";
ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate);
ifstream::pos_type fileSize = ifs.tellg();
ifs.seekg(0, ios::beg);
vector<char> bytes(fileSize);
ifs.read(bytes.data(), fileSize);
cout << bytes.data();;
cout << "\n";
cout << fileSize;
cout << "\n";
return bytes.data();
// return string(bytes.data(), fileSize);
}
Based on the output of cout << fileSize;
it shows 744402 bytes but when I print out bytes.data()
I only get the first 8 bytes, LIZM 2.9
. I used a hexdump tool to look into the binary file and noticed that the 9th byte is a null character. Hex dump of the first 16 bytes with the corresponding ASCIIs are shown below:
4C 49 5A 4D 20 32 2E 39 00 00 21 C4 00 00 00 00 LIZM 2.9 __!____
As you can see _
corresponds to the null character 00
. My question is how do I read every byte instead of stopping at the null character?