/*
Дан файл f, состоящий из информации об автомобиле: марка,
номер, цвет, номер двигателя, номер кузова, фамилия хозяина. Переписать из
файла f в файл f1 данные для заданной марки автомобиля черного цвета
*/
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
struct car_info
{
std::string brand;
int id;
std::string color;
int engine_id;
int body_id;
std::string owner;
};
std::istream & operator>>(std::istream & in, car_info & car) {
in >> car.brand >> car.id >> car.color >> car.engine_id >> car.body_id >> car.owner;
return in;
}
std::ostream & operator<<(std::ostream & out, const car_info & car) {
out << car.brand << " " << car.id << " " << car.color << " "
<< car.engine_id << " " << car.body_id << " " << car.owner;
return out;
}
int main()
{
std::ifstream input("f.txt");
if (!input.is_open()) {
std::cout << "Can't open file!\n";
return 0;
}
std::cout << "Car list:\n";
std::copy(std::istream_iterator<car_info>(input), {},
std::ostream_iterator<car_info>(std::cout, "\n"));
std::cout << "\n";
std::cout << "Enter brand to select cars: ";
std::string brand;
std::cin >> brand;
input.clear();
input.seekg(0, std::ios::beg);
std::ofstream output("f1.txt");
std::copy_if(std::istream_iterator<car_info>(input), {},
std::ostream_iterator<car_info>(output, "\n"), [brand] (const car_info & car) {
return (car.brand == brand) && (car.color == "black");
});
return 0;
}
bmw 100500 red 123456 65431 smith
audi 243500 black 456789 97531 harris
bmw 547698 black 123789 13579 murray
mersedes 124587 yellow 987654 65321 dickinson
audi 785600 blue 890654 89123 mcbrain
bmw 547698 black 123789 13579 murray