Требуется отредактировать записанные в бинарный файл значения, знаю что нужно использовать seek, но как именно это организовать не могу понять.
Нужно отредактировать "flight_destination"
<code lang="cpp">
struct Flight_Details {
char destination[99];
char departure[99];
char time_depart[80];
char time_arrive[80];
int flight_number;
};
int menu;
do {
cout << "***MENU***\n\n1. New Flight Option\n2. Output Flight Option\n3. Edit\n4. Exit\n\n";
cout << "Enter menu element: ";
cin >> menu;
switch (menu)
{
case MENU::NEW_FLIGHT: {
Flight_Details flight_d;
cout << "Enter Departure: ";
cin >> flight_d.destination;
cout << "Enter Destination: ";
cin >> flight_d.departure;
cout << "Enter Departure Time: ";
cin >> flight_d.time_depart;
cout << "Please enter arriving time: ";
cin >> flight_d.time_arrive;
cout << "Flight Number: ";
cin >> flight_d.flight_number;
ofstream file;
file.open("Flgiht_Details.data", ios::binary);
if (!file) cout << "Could create/open file";
else {
file.write((char*)&flight_d, sizeof(flight_d));
file.close();
}
break;
}
case MENU::OUTPUT_FILE: {
ifstream readFile;
readFile.open("Flgiht_Details.data");
if (!readFile) cout << "Couldn't open file";
else {
readFile.seekg(0, ios::end);
int fileSize = readFile.tellg();
int countOfFlights = fileSize / sizeof(Flight_Details);
readFile.seekg(0, ios::beg);
Flight_Details* flight_d = new Flight_Details[countOfFlights];
readFile.read((char*)flight_d, countOfFlights *sizeof(Flight_Details));
readFile.close();
for (int i = 0; i < countOfFlights; i++)
{
cout << flight_d[i].destination << "\n" << flight_d[i].departure << "\n" << flight_d[i].time_depart << "\n" << flight_d[i].time_arrive << "\n" << flight_d[i].flight_number << "\n\n";
}
break;
}
}
case MENU::EDIT: {
ifstream readFile;
readFile.open("Flgiht_Details.data", ios::binary | ios::in || ios::out);
if (!readFile) cout << "Couldn't open file";
else {
readFile.seekg(0, ios::end);
int fileSize = readFile.tellg();
int countOfFlights = fileSize / sizeof(Flight_Details);
readFile.seekg(0, ios::beg);
Flight_Details* flight_d = new Flight_Details[countOfFlights];
readFile.read((char*)flight_d, countOfFlights * sizeof(Flight_Details));
readFile.close();
char edit[50];
cin.ignore();
cout << "Edit: ";
cin.getline(edit, 50);
ofstream file;
file.open("Flgiht_Details.data", ios::binary | ios::in | ios::out);
for (int i = 0; i < countOfFlights; i++)
{
if (strcmp(flight_d[i].destination, edit) == 0)
{
strcpy_s(flight_d[i].destination, edit);
readFile.seekg(i* sizeof(Flight_Details));
file.write(reinterpret_cast<const char*>(&flight_d[i]),sizeof(Flight_Details));
file.close();
break;
}
}
}
break;
}
</code>