阅读/写作二进制文件的问题

发布于 2025-01-21 22:28:45 字数 1816 浏览 3 评论 0原文

我写了下面的代码,以将结构向量保存到二进制文件中。问题是在阅读所有书面数据之前,读数停止。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2025-01-28 22:28:45
binFile.open("test.bin", std::ios::binary);

输出文件以二进制模式打开。

binFile.open("test.bin", std::ifstream::in);

输入文件不是。

这仅在操作系统上很重要,该操作系统将其谱系追踪到MS-DOS,并且如果您的二进制文件恰好具有0x0D字节,则在阅读时将被删除。也以二进制模式打开输入文件。

binFile.open("test.bin", std::ios::binary);

The output file was opened in binary mode.

binFile.open("test.bin", std::ifstream::in);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文