定义Read函数
read函数以一定长度读取文件中的数据,并将其存储在新数组中。 用于从文件读取数据。
函数原型istreamread(char*s,streamsize n );
参数char* s表示提取数据流的char型数组指针,streamsize n表示数组的长度
官方链接http://www.cplusplus.com/reference/istream/istream/read /? kw=read
使用Read函数的示例
#include //std:cout
#include //std:ifstream
int main () )。
STD : ifstream is (' test.txt ',STD :3360 ifstream 33603360 binary );
if(is ) {
//get length of file:
is.seekg(0,is.end );
int length=is.tellg (;
is.seekg(0,is.beg );
char * buffer=new char [length];
STD :3360 cout ' reading ' length ' characters . ';
//read data as a block:
is.read (缓冲区,长度);
是if(is )
STD : cout ' allcharactersreadsuccessfully.';
else
STD : cout ' error : only ' is.gcount () ' could be read ';
is.close (;
//buffercontainstheentirefile .
delete[] buffer;
}
返回0;
}