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


#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int vibor;
    double x, sum;

    do
    {
        cout << "\nМЕНЮ\n";
        cout << "1 - Запись в файл\n";
        cout << "2 - Чтение файла\n";
        cout << "3 - Дозапись в файл\n";
        cout << "4 - Суммирование чисел\n";
        cout << "0 - Выход\n";
        cout << "Ваш выбор: ";
        cin >> vibor;

        // 1. Запись
        if (vibor == 1)
        {
            ofstream f("a.txt", ios::out);

            int n;
            cout << "Сколько чисел записать: ";
            cin >> n;

            for (int i = 0; i < n; i++)
            {
                cout << "x = ";
                cin >> x;

                f << x << " ";
            }

            f.close();
            cout << "Файл записан\n";
        }

        // 2. Чтение
        else if (vibor == 2)
        {
            ifstream f("a.txt", ios::in);

            cout << "Содержимое файла:\n";

            while (f >> x)
            {
                cout << x << " ";
            }

            cout << endl;

            f.close();
        }

        // 3. Дозапись
        else if (vibor == 3)
        {
            ofstream f("a.txt", ios::app);

            int n;
            cout << "Сколько чисел добавить: ";
            cin >> n;

            for (int i = 0; i < n; i++)
            {
                cout << "x = ";
                cin >> x;

                f << x << " ";
            }

            f.close();
            cout << "Данные добавлены\n";
        }

        // 4. Суммирование
        else if (vibor == 4)
        {
            ifstream f("a.txt", ios::in);

            sum = 0;

            while (f >> x)
            {
                sum = sum + x;
            }

            cout << "Сумма = " << sum << endl;

            f.close();
        }

    } while (vibor != 0);

    return 0;
}