打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c++ 里 istream read()的用法
c++里 istream read()的用法
2010-05-19 19:18

Read block of data

Reads a block of data of n characters and stores it in the array pointed by s.

If the End-of-File is reached before n characters have been read, the array will contain all the elements read until it, and the failbit and eofbit will be set (which can be checked with members fail and eof respectively).

Notice that this is an unformatted input function and what is extracted is not stored as a c-string format, therefore no ending null-character is appended at the end of the character sequence.

Calling member gcount after this function the total number of characters read can be obtained.

Parameters

s
Pointer to an allocated block of memory where the content read will be stored.
n
Integer value of type streamsize representing the size in characters of the block of data to be read.


Return Value

The functions return *this.

Errors are signaled by modifying the internal state flags:

flag error
eofbit The end of the source of characters is reached before n characters have been read. This also sets failbit.
failbit The end of the source of characters is reached before n characters have been read. This also sets eofbit.
badbit An error other than the above happened.


Additionaly, in any of these cases, if the appropriate flag has been set with member function ios::exceptions, an exception of type ios_base::failure is thrown.


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// read a file into memory
#include <iostream>
#include <fstream>
using namespace std;

int main () {
int length;
char * buffer;

ifstream is;
is.open ("test.txt", ios::binary );

// get length of file:
is.seekg (0, ios::end);
length = is.tellg();
is.seekg (0, ios::beg);

// allocate memory:
buffer = new char [length];

// read data as a block:
is.read (buffer,length);
is.close();

cout.write (buffer,length);

delete[] buffer;
return 0;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
c++中ifstream一次读取整个文件
BASE64转换,内含:IOS自带DES加解密
Response.ContentType 所有类型例举
IOS的网络通信的方法
[LeetCode] 158. Read N Characters Given Read4 II - Call multiple times
字符流相关函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服