Lab7 2
Lab7 2
Lab7 2
#include <fstream>
#include<iomanip>
struct Car {
char brand[nBrand+1];
char numberPlate[nNumber+1];
char ownerLastName[nOwner+1];
void input_data();
void output_data();
};
if (strcmp(cars[i].brand,targetBrand)==0) {
cars[i].output_data(); isBrand = true;
}
}
int main() {
char fname[] = "bin.txt";
ofstream outputFile("cars_data.txt", ios::app);
ofstream outFile_bin(fname,ios::binary|ios::app);
if (!outputFile.is_open()) {
cerr << "Unable to open file!" << endl;
return 1;
}
int numOfCarsToAdd;
cout << "Enter the number of cars you want to add: ";
cin >> numOfCarsToAdd;
Car newCar;
newCar.input_data();
outputFile << newCar.brand << " " << newCar.numberPlate << " " <<
newCar.ownerLastName << endl;
outFile_bin.write((char*)&newCar,sizeof(Car));
}
outputFile.close();
cout << "Car data successfully written to the file." << endl;
ifstream inputFile("cars_data.txt");
if (!inputFile.is_open()) {
cerr << "Unable to open file!" << endl;
return 1;
}
inputFile.close();
char targetBrand[50];
cout << "Enter car brand to display information: ";
cin >> targetBrand;
displayCarData(targetBrand, cars, carsCount);
system("pause");
return 0;
}
void Car::input_data()
{
cout << "Enter car brand: ";
cin .getline(brand,nBrand);
cout << "Enter car number plate: ";
cin .getline(numberPlate,nNumber);
cout << "Enter owner's last name: ";
cin.getline(ownerLastName,nOwner);
}
void Car::output_data()
{
cout << setw(nBrand) << brand << setw(nNumber) << numberPlate << setw(nOwner)
<< ownerLastName << endl;
}