ifstream :: read不追加'\ 0' [英] ifstream::read doesnot append '\0'
问题描述
ifstream :: read
只是将 n
个字节读入缓冲区,但不会在末尾附加'\ 0'
缓冲区,对不对?然后,当我使用缓冲区时,如何知道缓冲区的结尾?
ifstream::read
just reads n
bytes into a buffer, but doesn't append '\0'
at the end of the buffer, right? Then when I use the buffer, how does it know the end of the buffer?
是否应该在缓冲区末尾手动添加'\ 0'
?
Should I manually append a '\0'
at the end of the buffer?
推荐答案
ifstream
用于读取文件,二进制文件或文本.当使用 read
处理二进制文件时,不能确定零字节的起源(来自文件本身还是由 read
附加),因此read不会附加零.到目标缓冲区.
The ifstream
is used for reading from a file, binary or text. When dealing with a binary file with read
, one cannot be sure the origin of a zero byte (from the file itself or appended by read
) so read does not append a zero to destination buffer.
如果您正在处理文本文件,则可以使用 std :: getline ,并收到 std :: string
:
If you are working on a text file, then you can use std::getline, and receive a std::string
:
istream& getline ( istream& is, string& str );
这篇关于ifstream :: read不追加'\ 0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!