C++ 写入二进制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8329767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:23:01  来源:igfitidea点击:

writing into binary files

c++binaryfstream

提问by Wizard

#include <iostream>
#include <fstream>

using namespace std;

class info {

private:
    char name[15];
    char surname[15];
    int age;
public:
    void input(){
        cout<<"Your name:"<<endl;
            cin.getline(name,15);
        cout<<"Your surname:"<<endl;
        cin.getline(surname,15);
        cout<<"Your age:"<<endl;
        cin>>age;
        to_file(name,surname,age);
    }

    void to_file(char name[15], char surname[15], int age){
        fstream File ("example.bin", ios::out  | ios::binary | ios::app);
    // I doesn't know how to fill all variables(name,surname,age) in 1 variable (memblock) 
        //example File.write ( memory_block, size ); 

File.close();
    }

};

int main(){

info ob;
ob.input();

 return 0;
}

I don't know how to write more than 1 variable into a file, please help, I included an example ;) Maybe there are better ways to write to a file, please help me with this, it's to hard for me to solve.

我不知道如何将 1 个以上的变量写入文件,请帮忙,我包含了一个示例;) 也许有更好的写入文件的方法,请帮我解决这个问题,这对我来说很难解决。

回答by Johnsyweb

For a textfile, you could easily output one variable per line using a similar <<to the one you use with std::cout.

对于文本文件,您可以<<使用与std::cout.

For a binaryfile, you need to use std::ostream::write(), which writes a sequence of bytes. For your ageattribute, you'll need to reinterpret_castthis to const char*and write as many bytes as is necessary to hold an intfor your machine architecture. Note that if you intend to read this binary date on a different machine, you'll have to take word sizeand endiannessinto consideration. I also recommend that you zero the nameand surnamebuffers before you use them lest you end up with artefacts of uninitialised memory in your binary file.

对于二进制文件,您需要使用std::ostream::write(),它会写入一个字节序列。对于您的age属性,您需要将reinterpret_castconst char*写入并写入尽可能多的字节以容纳int您的机器架构。请注意,如果您打算在另一台机器上读取此二进制日期,则必须考虑字长字节序。我还建议您在使用namesurname缓冲区之前将它们归零,以免最终在二进制文件中出现未初始化内存的人工制品。

Also, there's no need to pass attributes of the class into the to_file()method.

此外,无需将类的属性传递到to_file()方法中。

#include <cstring>
#include <fstream>
#include <iostream>

class info
{
private:
    char name[15];
    char surname[15];
    int age;

public:
    info()
        :name()
        ,surname()
        ,age(0)
    {
        memset(name, 0, sizeof name);
        memset(surname, 0, sizeof surname);
    }

    void input()
    {
        std::cout << "Your name:" << std::endl;
        std::cin.getline(name, 15);

        std::cout << "Your surname:" << std::endl;
        std::cin.getline(surname, 15);

        std::cout << "Your age:" << std::endl;
        std::cin >> age;

        to_file();
    }

    void to_file()
    {
        std::ofstream fs("example.bin", std::ios::out | std::ios::binary | std::ios::app);
        fs.write(name, sizeof name);
        fs.write(surname, sizeof surname);
        fs.write(reinterpret_cast<const char*>(&age), sizeof age);
        fs.close();
    }
};

int main()
{
    info ob;
    ob.input();
}

A sample data file may look like this:

示例数据文件可能如下所示:

% xxd example.bin
0000000: 7573 6572 0000 0000 0000 0000 0000 0031  user...........1
0000010: 3036 3938 3734 0000 0000 0000 0000 2f00  069874......../.
0000020: 0000                                     ..

回答by Dani

File.write(name, 15);
File.write(surname, 15);
File.write((char *) &age, sizeof(age));