CFile VS CStdioFile

博文來自 http://blog.csdn.net/davidhsing/article/details/1759494 ide

MSDN: 函數

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. this

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. .net

Note: code

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

說明: ci

這幾段MSDN中的文字(來自主題:CStdioFile::ReadString)說明的問題是:
CFile類操做文件默認的是Binary模式,CStdioFile類操做文件默認的是Text模式。
在Binary模式下咱們必須輸入'/r/n',才能起到回車換行的效果,'/r'只是回車(回到當前行的第一列),'/n'只是換行(到達下一行的當前列)。
而在Text模式下'/r'的工做是自動完成的,當咱們寫一個'/n'的時候,系統會自動幫咱們在其前面加上一個'/r',而後保存到文件裏。 rem

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


如下為讀取文本的用法: io

CStdioFile m_pStream("d:\\1.txt", CFile::modeRead|CFile::shareDenyWrite);
CString strLine;
m_pStream.ReadString(strLine);////first line;
while(m_pStream.ReadString(strLine))//這裡是關鍵!不須要用m_pStream
{
 ///do some thing;
}
相關文章
相關標籤/搜索