Reading data from a file
Reading data from a file
Examples
Python |
Reading data |
Fin = open("26.txt") # open file
... # file operations
Fin.close() # close the file
|
Read a single number from a string
x = int(fin.readline())
If there are several numbers separated by a space, then it is necessary to break the string into separate parts
x, y = map(int, fin.readline().split())
|
C++ |
Reading data |
#include <fstream>
...
ifstream fin("26.txt");
fin >> x;
fin >> y >> z
|
Instead of cin use fin .
|
or you can just redirect standard input to a file
#include <iostream>
using namespace std;
main()
{
freopen fin("26.txt", "r", stdin);
// "r" - open file in read mode (read)
cin>> x;
...
}
|
You can use cin in the same way as when typing on the keyboard
|
Pascal |
Reading data |
redirect standard input to a file
Assign( input, '26.txt' );
|
read and readln , same as keyboard input |
Задача
The
file contains positive integers. The first line of the file contains the number
N
- amount of numbers. The following
N
lines contain the actual numbers.
Indicate in your answer 3 numbers separated by one space: the sum of all numbers, the number of even numbers and the sum of multiples of 4.
Выберите правильный ответ, либо введите его в поле ввода
Комментарий учителя