简体   繁体   中英

reading and writing binary files

I have a problem with binary files. I want to create a binary file in which there will be numbers of version and subversion of program. Then, from that file I want to read what is inside by reading first sizeof(uint16_t) bites and then again sizeof(uint16_t) bites. But I don't know how to do that. (I'm not even close)

#include <iostream>
#include <fstream>
#include <stdint.h>

using namespace std;

void   saving_uint16_t(uint16_t number);
uint16_t reading_uint16_t();

int where = 0;

int main() {

    const uint16_t number_version = 2;
    cout << "Version: " << number_version << endl;
    cout << "Saving number..." << endl;
    saving_uint16_t(number_version);

    const uint16_t number_subversion = 1;
    cout << "Subversion: " << number_subversion << endl;
    cout << "Saving number..." << endl;
    saving_uint16_t(number_subversion);

    cout << "Read numbers:\nVersion: " << reading_uint16_t() << "\nSubversion: " << reading_uint16_t() << endl << endl;

    return 0;
}

void saving_uint16_t(uint16_t number) {

    ofstream data("numbers.bin",  ios::app | ios::binary);
    data.write(reinterpret_cast<char*>(&number), sizeof(uint16_t));
}

uint16_t reading_uint16_t(){

    ifstream data("numbers.bin", ios::binary);
    data.seekg(where);
    where = where + 16;
    uint16_t result;
    data.read(reinterpret_cast<char*>(&result), sizeof(uint16_t));
    return result;
}

I'm really new with that and I don't know what to use.

Output:

Version: 3
Saving number...
Subversion: 7
Saving number...
Read numbers:
Version: 3
Subversion: 3

And then I changed numbers and still I get:

Version: 2
Saving number...
Subversion: 1
Saving number...
Read numbers:
Version: 3
Subversion: 3

It should be:

Version: 2
Saving number...
Subversion: 1
Saving number...
Read numbers:
Version: 2
Subversion: 1

And I want to add that my target is to create a binary file where there are two uint16_t variables and then to seperatly read them from file. So I can write that Version: (first uint16_t), Subversion: (second uint16_t)

So your understanding would probably be improved by looking at a function that extracted all the uint16_t s in numbers.bin. Presuming all that numbers.bin contains is uint16_t s then you could do something like this:

ifstream data("numbers.bin", ios_base::binary | ios_base::ate); // Starting at the end of the stream for sizing
const auto count = data.tellg() / sizeof(uint16_t); // tellg will report the number of characters in the file, sizeof(uint16_t) the numberof characters required to store a uint16_t, thus the division will give us the number of uint16_ts in numbers.bin
vector<uint16_t> numbers(count); // Allocate storage for all the uint16_ts in numbers.bin

data.seekg(0U, ios_base::beg); // Move the input position indicator to the beginning of the file for reading
data.read(reinterpret_cast<char*>(numbers.data()), count * sizeof(uint16_t)); // Slurp the file into numbers

Similarly to access a specific element of numbers.bin we'll need to set the position accordingly:

ifstream data("numbers.bin", ios_base::binary); // Starting at the beginning of the file
const auto where = 2U; // This offset will be 0 based
uint16_t number; // Allocate storage for the element at where

data.seekg(where * sizeof(uint16_t)); // Move the input position indicator to the specified uint16_t
data.read(reinterpret_cast<char*>(&number), sizeof(number)); // Read the element into number

Live Example

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