MFC中文件读写的几个函数

MFC中文件读写的几个函数 

2010-11-25 17:05:18|  分类: MFC |  标签: |字号 订阅

1、CFile::Read()函数:

virtual UINT Read( void* lpBuf, UINT nCount );
throw( CFileException );

lpBuf指向用户提供的缓冲区以接收从文件中读取的数据。
nCount可以从文件中读出的字节数的最大值。对文本模式的文件,回车换行作为一个换行字符(0x0A)字符,即读入内存时,0x0D0A变成0x0A。

2、CFile::Write()函数:

virtual void Write(     const void* lpBuf,     UINT nCount   );
Parameters
lpBuf

A pointer to the user-supplied buffer that contains the data to be written to the file.

nCount

The number of bytes to be transferred from the buffer. For text-mode files, carriage return–linefeed pairs are counted as single characters.

自我总结:此函数向文件中写字符串时,将换行符(0x0A)也转换成回车换行符(0X0D0A).对于文本文件格式都如此。

3、CStdioFile::ReadString()函数:

virtual LPTSTR ReadString(     LPTSTR lpsz,     UINT nMax   );  virtual BOOL ReadString(     CString& rString  );
Parameters
lpsz

Specifies a pointer to a user-supplied buffer that will receive a null-terminated text string.

nMax

Specifies the maximum number of characters to read, not counting the terminating null character.

rString

A reference to a CString object that will contain the string when the function returns.

Return Value

A pointer to the buffer containing the text data. NULL if end-of-file was reached without reading any data; or if boolean, FALSE if end-of-file was reached without reading any data.

自己总结:此函数从文件中读取字符串时,遇到回车换行符结束读取,并且回车换行符不读入到内存(对于以CString类对象为实参的函数),而对于第一个函数原型会将回车换行0x0D0A符转换成0x0A。遇到回车符(0x0D),不停止读,并且将回车符(0x0D)变成换行符(0x0A),直到遇到回车换行符或者文件结束符。

4、CStdioFile::WriteString()函数:

virtual void WriteString(
   LPCTSTR lpsz

);

Parameters
lpsz

Specifies a pointer to a buffer containing a null-terminated text string.

自我总结:此函数向文本文件写入字符时,遇到换行符(0x0A)将向文件中写入0x0D 0A即回车换行两个字符。而不会在lpsz所指向的字符串末尾加回车换行符。遇到0x0D只是向文件中写入0x0D。并不是将lpsz所指向的字符串作为一行写入。

从文件中读入数据时,如果实参是CString对象,那么系统会自动在此对象的字符缓冲区的最后加上1个字符结束符'\0',用字符串常量给此对象赋值时,也会自动在最后加上1个字符结束字符'\0';但是必须取得字符串内部缓冲区指针去访问此结束字符,如果通过成员函数str.GetAt(str.GetLength())去访问时会有一个断言错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值