std::istream::get的误用

今天想从流里面读数据到内存块里,写出如下代码:pStream->get(ba.data(), nCount),结果发现内存块里的数据错误,从某个字节以后都未赋值。仔细阅读了get函数的说明,明白错误出在了下面的第2条上。因为我读取的数据里有一个回车字符,用get函数会导致它自动截断。如果要从一个流读取内存块,正确的做法是用read方法。





std::istream::get

single character (1)
int get();
istream& get (char& c);
c-string (2)
istream& get (char* s, streamsize n);
istream& get (char* s, streamsize n, char delim);
stream buffer (3)
istream& get (streambuf& sb);
istream& get (streambuf& sb, char delim);
Get characters
Extracts characters from the stream, as  unformatted input:

(1) single character
Extracts a single character from the stream.
The character is either returned (first signature), or set as the value of its argument (second signature).
(2) c-string
Extracts characters from the stream and stores them in  s as a c-string, until either  (n-1) characters have been extracted or the  delimiting character is encountered: the  delimiting character being either the  newline character ( '\n') or  delim (if this argument is specified).
The  delimiting character is  not extracted from the input sequence if found, and remains there as the next character to be extracted from the stream (see  getline for an alternative that  does discard the  delimiting character).
null character ( '\0') is automatically appended to the written sequence if  n is greater than zero, even if an empty string is extracted.
(3) stream buffer
Extracts characters from the stream and inserts them into the output sequence controlled by the  stream buffer object  sb, stopping either as soon as such an insertion fails or as soon as the  delimiting character is encountered in the input sequence (the  delimiting character being either the newline character,  '\n', or  delim, if this argument is specified).
Only the characters successfully inserted into  sb are extracted from the stream: Neither the  delimiting character, nor eventually the character that failed to be inserted at  sb, are extracted from the input sequence and remain there as the next character to be extracted from the stream.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值