C++ifstreamを使用してテキストファイルから整数を読み取ります
これは、C++ifstreamを使用してテキストファイルから整数を読み取る例です。
例
#include <fstream>
#include<iostream>
using namespace std;
int main() {
//initialie the array size
int arr[30];
ifstream is("a.txt");
int cnt= 0;
int x;
// check that array is not already full
while (cnt < arr[30] && is >> x)
// and read integer from file
arr[cnt++] = x;
// print the integers stored in the array
cout<<"The integers are:"<<"\n";
for (int i = 0; i < cnt; i++) {
cout << arr[i] <<' ';
}
//close the file
is.close();
} 出力
The integers are: 1 2 3 4 5 6 7
-
Pythonを使用してテキストファイルから1行全体を読み取る方法は?
読み取り関数は、ファイル全体を一度に読み取ります。 readlines関数を使用して、ファイルを1行ずつ読み取ることができます。以下を使用して最初の行を読むことができます: f = open('my_file.txt', 'r+') print(f.readline())
-
Pythonを使用してテキストファイルからいくつかの文字を読み取る方法は?
ファイルの内容を読み取るには、f.read(size)を呼び出すことができます。これにより、ある量のデータが読み取られ、文字列として返されます。 sizeはオプションの数値引数です。サイズが省略または負の場合、ファイルの内容全体が読み取られて返されます。それ以外の場合は、最大サイズのバイトが読み取られて返されます。ファイルの終わりに達した場合、f.read()は空の文字列( )を返します。 したがって、10個のASCII文字を読み取りたい場合は、引数として10を渡すだけです。 例 >>> f = open('my_file', 'r') &g