我用ifstream打开一个文件流,逐字节的读取里面的内容,可是在遇到换行的情况下读出来的字节好像不一样了。请问是怎么回事?要给它转化成c语言的fopen打来文件吗?
你需要使用 binary 方式打开 istream 。否则换行符可能会被变换。
实际上 C 允许对 text stream 做许多变换,不过现在一般只能看到改换行符的:
A text stream is an ordered sequence of characters composed into lines, each line
consisting of zero or more characters plus a terminating new-line character. Whether the
last line requires a terminating new-line character is implementation-defined. Characters
may have to be added, altered, or deleted on input and output to conform to differing
conventions for representing text in the host environment. Thus, there need not be a oneto-one correspondence between the characters in a stream and those in the external
representation. Data read in from a text stream will necessarily compare equal to the data
that were earlier written out to that stream only if: the data consist only of printing
characters and the control characters horizontal tab and new-line; no new-line character is
immediately preceded by space characters; and the last character is a new-line character.
Whether space characters that are written out immediately before a new-line character
appear when read in is implementation-defined.
做一个buffer把它吃掉,就是一个缓冲作用