写入二进制文件

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

我不知道如何将多个变量写入文件,请帮忙,下面是一个例子;)也许有更好的方法来写入文件,请帮助我解决这个问题,因为对我来说太难了。


2
虽然与您的问题无关,但如果您多次调用 ob.input(),则会发现输入代码中存在错误。尝试在 cin>>age 后添加 std::cin.ignore(100, '\n'); - Robᵩ
2个回答

23
针对一个文本文件,你可以使用类似于std::cout<<方法,每行输出一个变量。
对于二进制文件,你需要使用std::ostream::write()方法来写入一系列字节。对于你的age属性,你需要将其转换为const char*类型并使用reinterpret_cast方法,然后写入足够容纳机器架构下int类型所需字节数的字节。请注意,如果您打算在不同的计算机上读取此二进制数据,则需要考虑字长字节序问题。我还建议在使用之前将namesurname缓冲区清零,以免在二进制文件中出现未初始化内存的痕迹。
此外,没有必要将类的属性传递到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();
}

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

% 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                                     ..

谢谢!也许有些超出范围,但有哪些工具可以检查二进制文件? - Minh Tran
@MinhTran:是的,这是一个新问题。也许可以看一下 https://stackoverflow.com/tags/xxd/info ? - johnsyweb

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

如果我有超过1个int变量,例如char name[15]; char surname[15]; int age, phone;,当写入File.write(name, 15); File.write(surname, 15); File.write((char *) &age&phone, sizeof(age&phone));时,会发生什么? - Wizard
@user1069874:不可以这样连接它们,必须分别写入:File.write(name, 15); File.write(surname, 15); File.write((char *) &age, sizeof(age)); File.write((char *) &phone, sizeof(phone)); - Daniel
也许更好的写法是 File.write((char*)this, sizeof(info)); - Wizard
@user1069874:你可以这样做,但是为什么要将所有变量作为参数传递给函数呢? - Daniel
嗯,我不知道哪种方法更好,当使用 File.write ((char*)this, sizeof (info)); 时,文件会占用更多的空间 /: - Wizard
@user1069874:这可能是由于对齐问题导致的。 - Daniel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接