logo

C fread() 関数

C fread() ファイル ストリームから指定された量のデータを読み取るために使用される標準ライブラリ関数です。内で定義されている fread() 関数は、ファイル ストリームから特定のサイズの指定された数の要素を読み取り、バッファ メモリに格納します。 fread() 関数によって読み取られる合計バイト数は、読み取られる要素の数に各要素のバイト単位のサイズを乗算したものです。

C fread() の構文

size_t fread (void * buffer , size_t size , size_t count , FILE * stream );>

ファイル位置インジケーターは、読み取られたバイト数だけ自動的に前方に移動します。読み取られるオブジェクトが構造体や複雑なデータ型など、簡単にコピーできない場合は、正しく動作しません。



パラメーター

    バッファ: 読み取られたデータが保存されるバッファ メモリ ブロックへのポインタを指します。サイズ: 各要素のサイズをバイト単位で表します。 count: 読み込む要素の数を指します。 stream: ファイルストリームへのポインタを指します。

戻り値

  • この関数は、ファイルから正常に読み取られた要素の数を返します。
  • 戻り値がカウントより小さい場合は、エラーが発生したか、ファイルの終わりに達したことを意味します。
  • size または count の値が 0 の場合、fread() は 0 を返し、他のアクションは実行しません。

注記: fread() 関数自体は、ファイルの終わりとエラーを区別する方法を提供しません。feof と ferror を使用して、どちらが発生したかを判断できます。

C fread() の例

例1

以下のプログラムは fread() 関数を示しています。

C








// C program to illustrate fread() function> #include> int> main()> {> >// File pointer> >FILE>* filePointer;> >// Buffer to store the read data> >char> buffer[100];> >// 'g4g.txt' file is opened in read mode> >filePointer =>fopen>(>'g4g.txt'>,>'r'>);> > >// Data is read from the file into the buffer> >// sizeof(buffer) specifies the size of each element to> >// be read 1 is the number of elements to read> >// filePointer is the file to read from> >while> (!>feof>(filePointer)) {> >fread>(buffer,>sizeof>(buffer), 1, filePointer);> >// Print the read data> >printf>(>'%s'>, buffer);> >}> >fclose>(filePointer);> >return> 0;> }>

>

>

ファイル g4g.txt に次のデータが含まれているとします。

Geeks : DS-ALgo Gfg : DP Contribute : writearticle>

次に、プログラムを実行すると、出力は次のようになります。

Javaでリストを反復する
Geeks : DS-ALgo Gfg : DP Contribute : writearticle>

例 2

この C プログラムは、ファイルのサイズまたはカウントが 0 の場合の fread() 関数の使用法を示します。

C




// C program to illustrate fread() function> // when size of the file or the value of count is equal to 0> #include> int> main()> {> >// File pointer> >FILE>* filePointer;> >// Buffer to store the read data> >char> buffer[100];> >// 'g4g.txt' file is opened in read mode> >filePointer =>fopen>(>'g4g.txt'>,>'r'>);> >// Case when count is equal to 0> >printf>(>'count = 0, return value = %zu '>,> >fread>(buffer,>sizeof>(buffer), 0, filePointer));> >// Case when size is equal to 0> >printf>(>'size = 0, return value = %zu '>,> >fread>(buffer, 0, 1, filePointer));> >return> 0;> }>

>

>

出力

count = 0, return value = 0 size = 0, return value = 0>

C++


グレートアンドラ



// C++ program to illustrate the vector container> #include> #include> using> namespace> std;> int> main()> {> >// 1d vector with initialization list> >vector<>int>>v1 = { 1, 2, 3, 4, 5 };>> >// 2d vector with size and element value initialization> >vectorint>> v2(3, ベクトル (3, 5)); // Push_back() v1.push_back(6) を使用して値を追加します。 // size() cout を使用して v1 を出力します<< 'v1: '; for (int i = 0; i cout << v1[i] << ' '; } cout << endl; // deleting value usign erase and iterators v1.erase(v1.begin() + 4); // printing v1 using iterators cout << 'v2: '; for (auto i = v1.begin(); i != v1.end(); i++) { cout << *i << ' '; } cout << endl; // printing v2 using range based for loop cout << 'v2:-' << endl; for (auto i : v2) { for (auto j : i) { cout << j << ' '; } cout << endl; } return 0; }>

>

>

出力

v1: 1 2 3 4 5 6 v2: 1 2 3 4 6 v2:- 5 5 5 5 5 5 5 5 5>