Reading Binary Files into an array of ints c++
Solution 1
To simplify read operation consider storing size
(i.e the number of elements in the array) before the data:
void bcdEncoder::writeBinaryFile(unsigned int packedBcdArray[], int size)
{
fstream binaryIo;
binaryIo.open("PridePrejudice.bin", ios::out| ios::binary | ios::trunc);
binaryIo.seekp(0);
binaryIo.write(&size, sizeof(size));
binaryIo.write((char*)packedBcdArray, size * sizeof(packedBcdArray[0]));
binaryIo.close();
}
The read would look something like:
void bcdEncoder::readBinaryFile(string fileName)
{
std::vector<unsigned int> myData;
int size;
fstream binaryIo;
binaryIo.open(fileName, ios::in | ios::binary | ios::trunc);
binaryIo.read(&size, sizeof(size)); // read the number of elements
myData.resize(size); // allocate memory for an array
binaryIo.read(myData.data(), size * sizeof(myData.value_type));
binaryIo.close();
// todo: do something with myData
}
Solution 2
If you're using C++, use the nice std library.
vector<unsigned int> bcdEncoder::readBinaryFile(string fileName)
{
vector<unsigned int> ret; //std::list may be preferable for large files
ifstream in{ fileName };
unsigned int current;
while (in.good()) {
in >> current;
ret.emplace_back(current);
}
return ret;
}
Writing is just as simple (for this we'll accept an int[]
but an std library would be preferable):
void bcdEncoder::writeBinaryFile(string fileName, unsigned int arr[], size_t len)
{
ofstream f { fileName };
for (size_t i = 0; i < len; i++)
f << arr[i];
}
Here's the same thing but with an std::vector
void bcdEncoder::writeBinaryFile(string fileName, vector<unsigned int> arr)
{
ofstream f { fileName };
for (auto&& i : arr)
f << i;
}
Justin Tennant
I am a system builder, with expertise on knowing performance and reliability on most modern computer components.I have some experience with coding, and I am slowly working on learning it all. HTML - Novice CSS - Novice Javascript - Experienced Python - Novice Lua - Novice Java - Experienced C# - Experienced C++ - Beginner
Updated on June 17, 2022Comments
-
Justin Tennant almost 2 years
I have a method which writes a binary file from an
int
array. (it could be wrong too)void bcdEncoder::writeBinaryFile(unsigned int packedBcdArray[], int size) { fstream binaryIo; binaryIo.open("PridePrejudice.bin", ios::out| ios::binary | ios::trunc); binaryIo.seekp(0); binaryIo.write((char*)packedBcdArray, size * sizeof(packedBcdArray[0])); binaryIo.seekp(0); binaryIo.close(); }
I need to now read that binary file back. And preferably have it read it back into another array of
unsigned int
s without any information loss.I have something like the following code, but I have no idea on how reading binary files really works, and no idea how to read it into an array of
int
s.void bcdEncoder::readBinaryFile(string fileName) { // myArray = my dnynamic int array fstream binaryIo; binaryIo.open(fileName, ios::in | ios::binary | ios::trunc); binaryIo.seekp(0); binaryIo.seekg(0); binaryIo.read((int*)myArray, size * sizeof(myFile)); binaryIo.close(); }
Question:
How to complete the implementation of the function that reads binary files?
-
Justin Tennant over 8 yearsI'm guessing now that my write is just completely wrong. First off, the &size in the write fails because it says its expecting a char, but overall, when i take in my packedBcd Array (which I know has the correct values in it) the values are not being written into the .bin file in any way that I understand, if at all.
-
Justin Tennant over 8 yearsI can't figure out a way to implement a similar read method without vectors, which I am not able to use in this project.
-
Olipro over 8 yearsAre you completely and utterly banned from using a
vector
, or you're just required to return anint[]
? -
Justin Tennant over 8 yearspart of the project goal was to apply previously made custom dynamic array class file to a new project.