简体   繁体   中英

Problems reading/writing to binary file

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;
    }
}
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM