📄 [c++] cstdiofile读写文本文件.txt
字号:
可用CStdioFile,如:(其中WriteString为写入一行,ReadString为读取一行文本)
CStdioFile dataStore;
if (file)
{
CString somecode; //也可采用LPTSTR类型,将不会删除文本中的\n回车符
BOOL bIsOk = dataStore.Open(strPath+"\\"+Filename,
CFile::modeCreate
| CFile::modeWrite
| CFile::shareDenyWrite
| CFile::typeText);
if (!bIsOk)
return FALSE;
// 读写网页文件,直到为空
while (file->ReadString(somecode) != NULL) //如果采用LPTSTR类型,读取最大个数nMax置0,使它遇空字符时结束
{
dataStore.WriteString(somecode);
dataStore.WriteString("\n"); //如果somecode采用LPTSTR类型,可不用此句
}
}
徐景周
// -------------------------------------------------
// ReadString读文件, WriteString写文件, CFile::modeNoTruncate追加一行的打开方式
CStdioFile dataStore;
CString somecode;
BOOL bIsOk = dataStore.Open( rxFile,
CFile::modeRead
| CFile::shareDenyWrite
| CFile::typeText);
if (!bIsOk)
return;
BOOL bIsExist = FALSE;
// 逐行读文本文件
while (dataStore.ReadString(somecode) != NULL)
{
//AfxMessageBox(somecode);
if (somecode.CompareNoCase(g_ARXPrfMgrName) == 0)
{
bIsExist = TRUE;
break;
}
}
dataStore.Close(); // 关闭文件
if (bIsExist != TRUE)
{
bIsOk = dataStore.Open( rxFile,CFile::modeCreate|CFile::modeNoTruncate // Append 一行的方法
|CFile::modeReadWrite
| CFile::shareDenyWrite
| CFile::typeText);
if (!bIsOk)
return;
dataStore.SeekToEnd();
dataStore.WriteString("\n");
dataStore.WriteString(g_ARXPrfMgrName);
dataStore.WriteString("\n");
dataStore.Close(); // 关闭文件
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -