繁体   English   中英

读写二进制文件

[英]reading and writing binary files

我的二进制文件有问题。 我想创建一个二进制文件,其中将包含版本号和程序版本号。 然后,从该文件中,我想先读取sizeof(uint16_t)位,然后再读取sizeof(uint16_t)位,以读取内部内容。 但是我不知道该怎么做。 (我什至没有亲近)

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

我真的很陌生,不知道该用什么。

输出:

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

然后我更改了数字,但仍然得到:

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

它应该是:

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

我想补充一点,我的目标是创建一个有两个uint16_t变量的二进制文件,然后从文件中分别读取它们。 所以我可以写那个Version:(first uint16_t),Subversion:(second uint16_t)

因此,通过查看一个提取numbers.bin中所有uint16_t的函数,您的理解可能会得到改善。 假设numbers.bin包含的所有内容都是uint16_t那么您可以执行以下操作:

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

类似地,要访问numbers.bin的特定元素,我们需要相应地设置位置:

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

现场例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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