C++ File Binary

论操作非文本文件时,std::ios::binary的重要性

今天在读取图片文件的时候,根据图片数据的高宽对图片数据进行读取,但是出现了图片数据读取不完整,但是文件已经到达末尾的情况,中间想过read是否有问题,后来查阅函数说明发现这个std::ios::binary属性。

最后恍然大悟,如果不以二进制方式来读取文件,一些特殊控制字符会被直接过滤掉,从而导致读取提前结束的情况。

member constantstands foraccess
in * input File open for reading: the internal stream buffer supports input operations.
out output File open for writing: the internal stream buffer supports output operations.
binary binary Operations are performed in binary mode rather than text.
ate at end The output position starts at the end of the file.
app append All output operations happen at the end of the file, appending to its existing contents.
trunc truncate Any contents that existed in the file before it is open are discarded.

参考:http://www.cplusplus.com/reference/fstream/ifstream/open/

原文地址:https://www.cnblogs.com/zhanghang-BadCoder/p/7066475.html