I'm trying to write a structure to a binary file. The structure consists of strings and integers. If there were no strings I'd just take the entire object and write it to the binary file normally, but if I do that now, the string can be read easily as well.
So I decided to write each of the attribute of the structure separately. I was working with the strings the same way like it's mentioned in this Stackoverflow answer.
This is the main function that is supposed to save the structure into the name.bin file.
void saveFileBin(std::string nameOfFile) {
Person people[SIZE_OF_ARRAY] =
{Person("Name1", "lastName2", Address("Street1", "City1", "111"), Date(1, 1, 1111)),
Person("Name2", "lastName2", 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");
}
};
and this is a function for writing each attribute into the 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());
}
The resulting file has everything in plain text readable.
Though if I instead in the main method call myFile.write((char*)people, sizeof(people));
then it shows up unreadable characters correctly but string variables are still normally read. That's why I went with converting into the string all variables and then writing it to a bin file altogether.
How come my method still shows all strings like it's not even binary file? Even though I have std::ios::binary as a parameter?
The output file contains this:
Name1lastName1Street11111City11111111Name2lastName2Street22222City22222222Name3lastName3Street33333City3333333
Whereas if I write the entire structure into the binary file, it looks like this:
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 ÌÌÌÌÌÌÌÌÌÌÌÌ
ÌÌÌÌ
EDIT: As requested here is a header for 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