MSDN:CFile和CStdioFile区别以及CString中的'\r\n'

调试《Read tiff from memory(从内存流读取TIFF)》一文的代码时为了模拟内存中的TIFF,采用CStdioFile将Tiff读取内存,然后作处理,奇怪的,对有的TIFF文件显示正常,有的不正常,一时找不知道什么原因,怀疑写的TIFF文件有问题。后来一个同事把CStdioFile改为CFile就好了,细查后发现是由于两个类读取模式不同造成的:

Difference between text mode file and binary mode file
File I/O operations take place in one of two translation modes, text or binary, depending on the mode in which the file is opened. Data files are usually processed in text mode. To control the file translation mode, you can:
 a. Retain the current default setting and specify the alternative mode only when you open selected files.
 b. Change the default translation mode directly by setting the global variable _fmode in your program. The initial default setting of _fmode is _O_TEXT, for text mode.

Text mode provides special processing for carriage return–linefeed pairs. When you write a newline character (0x0A) to a text-mode CStdioFile object, the byte pair (0x0D, 0x0A) is sent to the file. When you read, the byte pair (0x0A, 0x0D) is translated to a single 0x0A byte.

这几段MSDN中的文字(来自主题:CStdioFile::ReadString)说明的问题是:
CFile类操作文件默认的是Binary模式,CStdioFile类操作文件默认的是Text模式。
在Binary模式下我们必须输入'\r\n',才能起到回车换行的效果,'\r'只是回车(回到当前行的第一列),'\n'只是换行(到达下一行的当前列)。
而在Text模式下'\r'的工作是自动完成的,当我们写一个'\n'的时候,系统会自动帮我们在其前面加上一个'\r',然后保存到文件里。

另外,CFile::typeText只能在CFile的子类中使用,CFile自身的Read函数不能使用CFile::typeText和CFile::typeBinary

MSDN: CStdioFile::ReadString
Note: The CString version of this function removes the '/n' if present; the LPTSTR version does not.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值