我正在尝试将结构写入二进制文件。该结构由字符串和整数组成。如果没有字符串,我只是将整个对象带入正常情况下将其写入二进制文件,但是如果现在执行该操作,则也可以轻松读取字符串。
因此,我决定分别编写该结构的每个属性。我正在使用字符串the same way like it's mentioned in this Stackoverflow answer。
这是应该将结构保存到 name.bin 文件中的主要功能。
void saveFileBin(std::string nameOfFile) {
Person people[SIZE_OF_ARRAY] =
{Person("Name1","lastName2",Address("Street1","City1","111"),Date(1,1,1111)),Person("Name2",Address("Street2","City2","222"),Date(2,2,2222)),Person("Name3","lastName3",Address("Street3","City3","333"),Date(3,3,3333))};
std::ofstream myFile(nameOfFile + ".bin",std::ios::binary);
if (myFile.is_open())
{
for (int i = 0; i < SIZE_OF_ARRAY; i++)
{
people[i].write(&myFile);
}
myFile.close();
std::cout << "The entire thing is in memory";
}
else
{
throw std::exception("Unable to open file");
}
};
这是用于将每个属性写入文件的功能。
void Person::write(std::ofstream* out)
{
out->write(_name.c_str(),_name.size());
out->write(_lastName.c_str(),_lastName.size());
out->write(_residence.getStreet().c_str(),_residence.getStreet().size());
out->write(_residence.getZip().c_str(),_residence.getZip().size());
out->write(_residence.getcity().c_str(),_residence.getcity().size());
std::string day = std::to_string(_birthDate.getDay());
std::string month = std::to_string(_birthDate.getMonth());
std::string year = std::to_string(_birthDate.getYear());
out->write(day.c_str(),day.size());
out->write(month.c_str(),month.size());
out->write(year.c_str(),year.size());
}
生成的文件具有纯文本可读的所有内容。
尽管如果我改为在主方法调用myFile.write((char*)people,sizeof(people));
中使用它,则它可以正确显示不可读的字符,但仍可以正常读取字符串变量。这就是为什么我将所有变量都转换为字符串,然后将其完全写入bin文件的原因。
为什么我的方法仍然显示所有字符串,甚至不是二进制文件?即使我将std :: ios :: binary作为参数?
输出文件包含以下内容:
Name1lastName1Street11111City11111111Name2lastName2Street22222City22222222Name3lastName3Street33333City3333333
如果我将整个结构写入二进制文件,则如下所示:
lÊ87 Name1 ÌÌÌÌÌÌÌÌÌÌ à^Ê87 lastName1 ÌÌÌÌÌÌ Ð_Ê87 Street1 ÌÌÌÌÌÌÌÌ ÐdÊ87 City1 ÌÌÌÌÌÌÌÌÌÌ bÊ87 111 ÌÌÌÌÌÌÌÌÌÌÌÌ W ÌÌÌÌ`kÊ87 Name2 ÌÌÌÌÌÌÌÌÌÌ fÊ87 lastName2 ÌÌÌÌÌÌ €iÊ87 Street2 ÌÌÌÌÌÌÌÌ PbÊ87 City2 ÌÌÌÌÌÌÌÌÌÌ ÐiÊ87 222 ÌÌÌÌÌÌÌÌÌÌÌÌ ® ÌÌÌÌ€dÊ87 Name3 ÌÌÌÌÌÌÌÌÌÌ `Ê87 lastName3 ÌÌÌÌÌÌ p`Ê87 Street3 ÌÌÌÌÌÌÌÌ gÊ87 City3 ÌÌÌÌÌÌÌÌÌÌ ðbÊ87 333 ÌÌÌÌÌÌÌÌÌÌÌÌ
ÌÌÌÌ lÊ87 Name1 ÌÌÌÌÌÌÌÌÌÌ à^Ê87 lastName1 ÌÌÌÌÌÌ Ð_Ê87 Street1 ÌÌÌÌÌÌÌÌ ÐdÊ87 City1 ÌÌÌÌÌÌÌÌÌÌ bÊ87 111 ÌÌÌÌÌÌÌÌÌÌÌÌ W ÌÌÌÌ`kÊ87 Name2 ÌÌÌÌÌÌÌÌÌÌ fÊ87 lastName2 ÌÌÌÌÌÌ €iÊ87 Street2 ÌÌÌÌÌÌÌÌ PbÊ87 City2 ÌÌÌÌÌÌÌÌÌÌ ÐiÊ87 222 ÌÌÌÌÌÌÌÌÌÌÌÌ ® ÌÌÌÌ€dÊ87 Name3 ÌÌÌÌÌÌÌÌÌÌ `Ê87 lastName3 ÌÌÌÌÌÌ p`Ê87 Street3 ÌÌÌÌÌÌÌÌ gÊ87 City3 ÌÌÌÌÌÌÌÌÌÌ ðbÊ87 333 ÌÌÌÌÌÌÌÌÌÌÌÌ
ÌÌÌÌ lÊ87 Name1 ÌÌÌÌÌÌÌÌÌÌ à^Ê87 lastName1 ÌÌÌÌÌÌ Ð_Ê87 Street1 ÌÌÌÌÌÌÌÌ ÐdÊ87 City1 ÌÌÌÌÌÌÌÌÌÌ bÊ87 111 ÌÌÌÌÌÌÌÌÌÌÌÌ W ÌÌÌÌ`kÊ87 Name2 ÌÌÌÌÌÌÌÌÌÌ fÊ87 lastName2 ÌÌÌÌÌÌ €iÊ87 Street2 ÌÌÌÌÌÌÌÌ PbÊ87 City2 ÌÌÌÌÌÌÌÌÌÌ ÐiÊ87 222 ÌÌÌÌÌÌÌÌÌÌÌÌ ® ÌÌÌÌ€dÊ87 Name3 ÌÌÌÌÌÌÌÌÌÌ `Ê87 lastName3 ÌÌÌÌÌÌ p`Ê87 Street3 ÌÌÌÌÌÌÌÌ gÊ87 City3 ÌÌÌÌÌÌÌÌÌÌ ðbÊ87 333 ÌÌÌÌÌÌÌÌÌÌÌÌ
ÌÌÌÌ
编辑: 根据要求,这里是Person.h的标头
#pragma once
#ifndef PERSON_H
#define PERSON_H
#include <string.h>
#include "Address.h"
#include "Date.h"
#include <fstream>
struct Person {
public:
Person(std::string name,std::string last_name,Address _residence,Date birthDate);
Person();
friend std::ostream& operator<<(std::ostream& os,const Person& p);
friend std::istream& operator>>(std::istream& is,Person& p);
std::string getName() const { return _name; }
std::string getLastName() const { return _lastName; };
Address getResidence() const { return _residence; };
Date getDate() const { return _birthDate; };
void read(std::ifstream *in);
void write(std::ofstream *out);
private:
std::string _name;
std::string _lastName;
Address _residence;
Date _birthDate;
};
#endif // !PERSON_H