Cpp-standard-library-cpp-basic-ios-read
提供:Dev Guides
C ++ basic_iosライブラリ-読み取り
説明
ストリームからn文字を抽出し、sが指す配列に格納するために使用されます。
宣言
- 以下は、std
- basic_istream :: readの宣言です。
basic_istream& read (char_type* s, streamsize n);
パラメーター
- n -sに書き込む最大文字数(終端のヌル文字を含む)。
- s -抽出された文字が保存されている配列へのポインタ。
戻り値
basic_istreamオブジェクト(* this)を返します。
例外
基本保証-例外がスローされた場合、オブジェクトは有効な状態です。
データの競合
sが指す配列内の要素とストリームオブジェクトを変更します。
例
- 以下のstd
- basic_istream :: readの例。
#include <iostream>
#include <fstream>
int main () {
std::ifstream is ("test.txt", std::ifstream::binary);
if (is) {
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = new char [length];
std::cout << "Reading " << length << " characters... ";
is.read (buffer,length);
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
delete[] buffer;
}
return 0;
}
出力は次のようになります-
Reading 640 characters... all characters read successfully.