Answer the question
In order to leave comments, you need to log in
How to fix a bug with line-by-line reading from a C++ binary file?
void input() {
ofstream In("AutoBase.txt", ios::binary);
char k = '+';
AutoBase a;
string Read;
while (k == '+') {
cout << "Enter name of car: "; getline(cin, a.Name);
cout << "Enter Release Date with a point '10.10.2010': "; getline(cin, Read);
a = Divide(a, Read, 0);
cout << "Enter Sale Release Date with a point '10.10.2010': "; getline(cin, Read);
a = Divide(a, Read, 1);
cout << "Enter '+' in case you want to contnue: "; cin >> k;
In.write((char*)&a, sizeof(AutoBase));
cin.ignore();
}
In.close();
}
AutoBase Divide(AutoBase a, string Read, bool b) {
int pos1 = Read.find('.');
int pos2 = Read.rfind('.');
if (!b) {
a.Release.day = stoi(Read.substr(0, pos1));
a.Release.mounth = stoi(Read.substr(pos1 + 1, pos2));
a.Release.year = stoi(Read.substr(pos2 + 1));
}
else {
a.SaleRelease.day = stoi(Read.substr(0, pos1));
a.SaleRelease.mounth = stoi(Read.substr(pos1 + 1, pos2));
a.SaleRelease.year = stoi(Read.substr(pos2 + 1));
}
return a;
}
struct Data {
int year;
int mounth;
int day;
};
struct AutoBase {
string Name;
Data Release;
Data SaleRelease;
};
void output(string name) {
ifstream Out(name, ios::binary);
AutoBase a;
while (Out.read((char*)&a, sizeof(AutoBase))) {
cout << endl << a.Name << " " << a.Release.day << "." << a.Release.mounth << "." << a.Release.year << " " <<
a.SaleRelease.day << "." << a.SaleRelease.mounth << "." << a.SaleRelease.year;
}
Out.close();
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question