使用 CFile 的子类 CStdioFile 的注意事项

目前为止只用到了 ReadString,也了解了一下 WriteString。

由于程序需要,本来程序中是用的CFile, 但是需要逐行读取文件数据,所以谷歌找到了 ReadString 类 —— 继承于CFile,是CFile 的派生类

当使用 WriteString 往文件中存以行为单位的数据时,后面不要加 \r 或 \n,因为你已经告诉程序了,这是一行的数据,它会自动加上行符号。否则,当使用 ReadString 读取以 WriteString 存储的数据时,数据会多出一个 \r .

以下是个人使用。

CString strPath;
CString strRead;
TCHAR PathPro[256] = {0};
GetCurrentDirectoryW(256,PathPro);                    //PathPro: current dir path
strPath = CString(PathPro)+_T("\\") + SCRIPT_FILE_NAME;  //SCRIPT_FILE_NAME: "User_Script.txt"

CStdioFile f;  
CFileException e;  
if(!f.Open(strPath,CFile::modeRead) )
    return FALSE;  
while(f.ReadString(strRead))              //如果文件未读完,返回true,否则返回false。
{
    CString strTemp;
    strTemp.Format(_T("\n{{ %s }}"),strRead);    //检测是否成功读出每行数据
    LogToFiles(strTemp,0);
}
f.Close();

猜你喜欢

转载自www.cnblogs.com/JanSN/p/9243860.html