阅读/写作二进制文件的问题
我写了下面的代码,以将结构向量保存到二进制文件中。问题是在阅读所有书面数据之前,读数停止。
用reinterpret_cast< const char*>
用(char*)
或或(const char*)
不帮助或更改输出。
看到它确实读取正确的数据,它读取代码至少部分正确。
我很明显吗?还是为什么这不起作用?
#include <iostream>
#include <fstream>
#include <vector>
struct Pixel {
long long data;
Pixel() {
data = 0;
}
Pixel(long long data) :
data(data)
{}
};
void saveToBin(std::vector<Pixel>* pixels) {
std::ofstream binFile;
binFile.open("test.bin", std::ios::binary);
if (!binFile.is_open()) {
perror("Error open");
exit(EXIT_FAILURE);
}
for (int i = 0; i < pixels->size(); i++) {
binFile.write(reinterpret_cast<const char*>(&pixels->at(i)),sizeof(Pixel));
}
binFile.close();
}
void readBin(std::vector<Pixel>* pixels) {
std::ifstream binFile;
binFile.open("test.bin", std::ifstream::in);
if (!binFile.is_open()) {
perror("Error open");
exit(EXIT_FAILURE);
}
Pixel p;
while (binFile.read(reinterpret_cast<char*>(&p), sizeof(Pixel))) {
pixels->push_back(p);
}
binFile.close();
}
int main()
{
std::vector<Pixel> vecOrig;
for (int i = 0; i < 1000; i++) {
vecOrig.push_back(Pixel(i*7));
}
std::vector<Pixel> vecRead;
saveToBin(&vecOrig);
readBin(&vecRead);
std::cout << "starting length : " << vecOrig.size() << " , read length : " << vecRead.size() << std::endl;
for (int i = 0; i < std::min(vecOrig.size(), vecRead.size()); i++) {
std::cout << vecOrig[i].data << " -> " << vecRead[i].data << std::endl;
}
}
I wrote the code below to save a vector of struct to a binary file. The problem is the reading stops before all the written data is read.
Changing the reinterpret_cast<const char*>
with (char*)
or (const char*)
does not help or change the output.
Seeing as it does read the right number for the data it does read the code is at least partially right.
Is there something obvious i'm missing? Or why does this not work?
#include <iostream>
#include <fstream>
#include <vector>
struct Pixel {
long long data;
Pixel() {
data = 0;
}
Pixel(long long data) :
data(data)
{}
};
void saveToBin(std::vector<Pixel>* pixels) {
std::ofstream binFile;
binFile.open("test.bin", std::ios::binary);
if (!binFile.is_open()) {
perror("Error open");
exit(EXIT_FAILURE);
}
for (int i = 0; i < pixels->size(); i++) {
binFile.write(reinterpret_cast<const char*>(&pixels->at(i)),sizeof(Pixel));
}
binFile.close();
}
void readBin(std::vector<Pixel>* pixels) {
std::ifstream binFile;
binFile.open("test.bin", std::ifstream::in);
if (!binFile.is_open()) {
perror("Error open");
exit(EXIT_FAILURE);
}
Pixel p;
while (binFile.read(reinterpret_cast<char*>(&p), sizeof(Pixel))) {
pixels->push_back(p);
}
binFile.close();
}
int main()
{
std::vector<Pixel> vecOrig;
for (int i = 0; i < 1000; i++) {
vecOrig.push_back(Pixel(i*7));
}
std::vector<Pixel> vecRead;
saveToBin(&vecOrig);
readBin(&vecRead);
std::cout << "starting length : " << vecOrig.size() << " , read length : " << vecRead.size() << std::endl;
for (int i = 0; i < std::min(vecOrig.size(), vecRead.size()); i++) {
std::cout << vecOrig[i].data << " -> " << vecRead[i].data << std::endl;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
输出文件以
二进制
模式打开。输入文件不是。
这仅在操作系统上很重要,该操作系统将其谱系追踪到MS-DOS,并且如果您的二进制文件恰好具有
0x0D
字节,则在阅读时将被删除。也以二进制模式打开输入文件。The output file was opened in
binary
mode.The input file was not.
This only matters on operating systems that trace their lineage to MS-DOS, and if your binary file happens to have a
0x0D
byte it will be gratuously deleted, when read. Open the input file in binary mode, too.