Загрузка данных


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    setlocale(LC_ALL, "Russian");

    string search_query;
    cout << "Введите название города для поиска: ";
    getline(cin, search_query);

    if (search_query.empty()) {
        cout << "Запрос пуст." << endl;
        return 0;
    }

    string fileName = "";
    char firstChar = tolower(search_query[0]);
    fileName += firstChar;
    fileName += ".txt";


    string lower_query = search_query;
    for (int i = 0; i < lower_query.length(); i++) {
        lower_query[i] = tolower(lower_query[i]);

    ifstream input(fileName);
    ofstream output("result.txt");

    if (!input.is_open()) {
        cout << "Файл " << fileName << " не найден. Скорее всего, городов на эту букву нет." << endl;
        return 1;
    }

    string line;
    bool found = false;

    while (getline(input, line)) {
        string lower_line = line;
        for (int i = 0; i < lower_line.length(); i++) {
            lower_line[i] = tolower(lower_line[i]);
        }


        if (lower_line.find(lower_query) != string::npos) {
            output << line << endl;
            found = true;
        }
    }

    if (found) {
        cout << "Готово! Результаты из файла " << fileName << " сохранены в result.txt" << endl;
    }
    else {
        cout << "В файле " << fileName << " ничего не найдено." << endl;
    }

    input.close();
    output.close();

    return 0;
}