Страница 1 из 1

Парсинг файла: ключ = значение

Добавлено: 07 июн 2024, 06:01
ya

Код: Выделить всё

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>

int main() {
    std::ifstream file("input.txt");
    if (!file.is_open()) {
        std::cerr << "Error opening file!" << std::endl;
        return 1;
    }

    std::vector<std::pair<std::string, std::string>> data;

    std::string line;
    while (std::getline(file, line)) {
        std::istringstream iss(line);
        std::string key, value;
        if (std::getline(iss, key, '=') && std::getline(iss, value)) {
            data.push_back(std::make_pair(key, value));
        }
    }

    for (const auto& pair : data) {
        std::cout << pair.first << " = " << pair.second << std::endl;
    }

    return 0;
}
Компиляция

Код: Выделить всё

g++ -std=c++11 program.cpp -o program