ifstream ofstream 读写文件异常

1. 使用ifstream读文件时被截断

ifstream file;

使用文本方式读时,即file.open(filePath, ifstream::in),如果碰到字符0x1A(SUB 换置)时就停止读,后面的内容就被截断了

使用二进制读可以解决此问题,即 file.open(strFilePath.c_str(), ifstream::in | std::ios::binary);


2.使用ofstream写文件时内容被修改

ofstream file;file

使用文本方式写时,即file.open(filePath, ofstream::out),如果碰到0x0A(LF 换行),会替换成0x0D 0x0A写到文件中。

使用二进制写可以解决此问题,即file.open(filePath, ofstream::out | std::ios::binary);


你可能感兴趣的:(c/c++编程)