今天用ACE_OS::fopen函数遇到一个很隐蔽的问题,我用ACE读一个二进制文件,
然后把内容写到另外一个文件中,和原来的内容不一样了,0A前面加了个0D。
后来研究了ACE的源代码,才把这个问题搞清楚,ACE_OS::fopen实际上封装的是
c函数库的fopen函数,我写二进制文件,调用函数的代码如下:
ACE_OS::fopen(sFileName.c_str(),"at+");
t表示文本格式的文件,a+表示
Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
然后调用ACE_OS::fwrite,在windows操作系统下,这个函数会自动在0A(/n)前面加上0D(/r),导致结果和原始文件的内容不一样。
要想避免这个问题,要保证对二进制文件读写时调用:
ACE_OS::fopen(sFileName.c_str(),"ab+");
这样就OK了。