Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "fstream"
- #include "sstream"
- using namespace std;
- struct car {
- char* name;
- float price;
- int year;
- };
- string * split_car_data(string line){
- string arr[3];
- auto i = 0;
- stringstream ssin(line);
- while (ssin.good() && i < 4){
- ssin >> arr[i];
- ++i;
- }
- return arr;
- }
- int main() {
- fstream file;
- vector<car> cars;
- vector <string> lines;
- string line;
- auto file_path = "/Users/toczekmj/CLionProjects/testy/pojazdy.txt";
- auto changes = false;
- file.open(file_path);
- if(file.is_open())
- {
- while(getline(file, line))
- {
- auto data = split_car_data(line);
- auto name = data[0];
- auto year = stoi((string)data[1]);
- auto price = stof((string)data[2]);
- char * n_name = new char[name.length()+1];
- std::strcpy(n_name, name.c_str());
- if(year >= 2016 && year <= 2021){
- price -= (price/10);
- changes = true;
- }
- cars.push_back(
- car{
- n_name,
- price,
- year
- }
- );
- }
- file.close();
- }
- if(changes){
- file.open(file_path, fstream::out | fstream::trunc);
- if(file.is_open())
- {
- for_each(cars.begin(), cars.end(), [&](const car &item) {
- file << item.name << " " << item.year << " " << item.price << "\n";
- });
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement