C++ ifstream ofstream 注意事项

很久没写C++,已经完全不会写了...

在使用ifstream读取一个二进制文件时,发现读取的内容和源文件不相同,导致数据解析失败,于是尝试把用ifstream读取的内容用ofstream写入另一个文件来对比

发现两个问题:

1. ofstream在0x0A之前自动添加了0x0D,原因是打开方式未设定为std::ios_base::binary,导致把换行符LF改成了CR/LF

Your stream is opening in text mode, and since 0x0A is the line feed (LF) character, that's being converted by your stream to 0x0D 0x0A, i.e. CR/LF.

http://stackoverflow.com/questions/5173498/why-does-ofstream-insert-a-0x0d-byte-before-0x0a

 

2.ifstream在读取到0x1A时自动触发EOF,此时,char数组里剩下空间全部填充为0xCD,导致读取内容不完整.0xCD为申请数组时new出来未用到空间,编译器会将这样的空间用0xCD填充

于是改为使用fread去读文件

http://blog.csdn.net/zssureqh/article/details/7684529

http://bbs.csdn.net/topics/350140248

posted @ 2016-07-06 17:46  wmalloc  阅读(373)  评论(0编辑  收藏  举报