MFC中一行一行地读文件
1、方法1:
CStdioFile file; CString strContent; CString temp; char* old_locale = _strdup( setlocale(LC_CTYPE,NULL) ); setlocale( LC_CTYPE, "chs" ); if(NULL != file.Open(path.GetBuffer(), CFile::modeRead)) { while (file.ReadString(temp)) { strContent += temp; strContent += "\n"; } file.Close(); } else { AfxMessageBox(L"文件打开失败"); } setlocale( LC_CTYPE, old_locale ); free( old_locale );
2、方法2
CFile file; char strLine[505]; CString strContent; f(NULL != file.Open(path.GetBuffer(), CFile::modeRead)) { int len = 0; CString temp; while ((len = file.Read(strLine, 500)) ) { strLine[len] = '\0'; strContent += strLine; } file.Close(); } else { AfxMessageBox(_T("文件打开失败")); }