Files
learning/cpp/Файлы/main.cpp
2022-04-28 04:34:45 +03:00

48 lines
979 B
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
#include <fstream>
using namespace std;
ifstream in_stream;
ofstream out_stream;
int main() {
in_stream.open("inp.txt");
out_stream.open("out.txt");
// Чтение файла
int N;
in_stream >> N;
cout << "Полученное число из файла: ";
cout << N << endl;
// Чтение двух чисел с одной строки:
int x;
in_stream >> x;
cout << "Первое число второй строки: " << x << endl;
int y;
in_stream >> y;
cout << "Второе число второй строки: " << y << endl;
// Чтение до конца файла:
int sum = 0;
int tmp;
while(!in_stream.eof()) {
in_stream >> tmp;
sum += tmp;
}
/* Аналог:
while (in_stream >> tmp) {
sum += tmp;
}
*/
cout << "Сумма оставшихся чисел в файле: " << sum << endl;
// Вывод в файл:
out_stream << sum << endl;
in_stream.close();
out_stream.close();
return 0;
}