[英]Reading Binary Files into an array of ints c++
I have a method which writes a binary file from an int
array.我有一个从int
数组写入二进制文件的方法。 (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.最好让它读回另一个unsigned int
数组,而不会丢失任何信息。
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.我有类似下面的代码,但我不知道读取二进制文件是如何工作的,也不知道如何将它读入int
数组。
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();
}
How to complete the implementation of the function that reads binary files?如何完成读取二进制文件的function的实现?
If you're using C++, use the nice std library. 如果您使用的是C ++,请使用漂亮的std库。
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): 编写同样简单(为此,我们将接受int[]
但最好使用std库):
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
这是同一件事,但带有std::vector
void bcdEncoder::writeBinaryFile(string fileName, vector<unsigned int> arr)
{
ofstream f { fileName };
for (auto&& i : arr)
f << i;
}
To simplify read operation consider storing size
(ie the number of elements in the array) before the data: 为了简化读取操作,请考虑在数据之前存储size
(即数组中元素的数量):
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
}
Thanks for the tips guys, looks like I worked it out!! 谢谢大家的提示,看来我已经解决了!! A major part of my problem was that half the arguments and syntax I added to the methods were not required, and actually messed things up. 我的问题的主要部分是我添加到方法中的参数和语法中的一半不是必需的,实际上使事情变得混乱。 Here are my working methods. 这是我的工作方法。
void bcdEncoder::writeBinaryFile(unsigned int packedBcdArray[], int size, string fileName)
{
ofstream binaryIo;
binaryIo.open(fileName.substr(0, fileName.length() - 4) + ".bin", ios::binary);
if (binaryIo.is_open()) {
binaryIo.write((char*)packedBcdArray, size * sizeof(packedBcdArray[0]));
binaryIo.close();
// Send binary file to reader
readBinaryFile(fileName.substr(0, fileName.length() - 4) + ".bin", size);
}
else
cout << "Error writing bin file..." << endl;
}
And the read: 和读:
void bcdEncoder::readBinaryFile(string fileName, int size)
{
AllocateArray packedData(size);
unsigned int *packedArray = packedData.createIntArray();
ifstream binaryIo;
binaryIo.open(fileName, ios::binary);
if (binaryIo.is_open()) {
binaryIo.read((char*)packedArray, size * sizeof(packedArray[0]));
binaryIo.close();
decodeBCD(packedArray, size * 5, fileName);
}
else
cout << "Error reading bin file..." << endl;
}
With the AllocateArray being my class that creates dynamic arrays without vectors somewhat safely with destructors included. 以AllocateArray为我的类,它可以安全地创建带有向量的动态数组,并带有析构函数。
std::array
使用std::array
的现代替代方案Here's a code snippet that uses more modern C++ to read a binary file into an std::array
.这是一个代码片段,它使用更现代的 C++ 将二进制文件读入std::array
。
const int arraySize = 9216; // Hard-coded
std::array<uint8_t, arraySize> fileArray;
std::ifstream binaryFile("<my-binary-file>", std::ios::in | std::ios::binary);
if (binaryFile.is_open()) {
binaryFile.read(reinterpret_cast<char*>(fileArray.data()), arraySize);
}
Because you're using an std::array
you'll need to know the exact size of the file during compile-time.因为您使用的是std::array
,所以您需要在编译时知道文件的确切大小。 If you don't know the size of the file ahead of time (or rather, you'll need to know that the file has at least X bytes available), use a std::vector
and look at this example here: https://stackoverflow.com/a/36661779/1576548如果您不提前知道文件的大小(或者更确切地说,您需要知道该文件至少有 X 字节可用),请使用std::vector
并在此处查看此示例: https: //stackoverflow.com/a/36661779/1576548
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.