一:寫文檔
1 創建文檔並寫入內容
CString filePath=L"C:\\unicode.txt";
CStdioFile wfile;
if (!wfile.Open(filePath,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary))
{
AfxMessageBox(L"文件無法修改");
return;
}
WORD sign=0xfeff; // unicode文檔 標誌
wfile.Write(&sign,2);
CString fileContent;
fileContent=L"UNICODE文檔";
wfile.Write(fileContent.GetBuffer(),fileContent.GetLength()*2);
fileContent.ReleaseBuffer();
wfile.Close();
WORD sign=0xfeff;
wFile.Write(&sign,2);
//遍歷list容器 寫入文件
list<CString>::iterator it=fileList.begin();
for (it=fileList.begin();it!=fileList.end();it++)
{
str=*it;
//寫入CString類型的字符串
wFile.Write(str.GetBuffer(),str.GetLength()*2);
str.ReleaseBuffer();
//使用WriteString 寫入回車換行符
wFile.WriteString(L"\r\n");
}
wFile.Close();
2 判斷文檔是否存在,若存在,則在文檔末尾添加內容,若不存在,重新創建
CString filePath=L"c:\\test.txt";
CStdioFile wfile;
CFileFind fileFind;
if(!fileFind.FindFile(filePath) // 查找文件是否存在
{
if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeCreate|CFile::typeBinary)) //unicode 必須以二進制打開
return ;
WORD sign=0xfeff;
wfile.Write(&sign,2);
}else
if (!wfile.Open(filePath,CFile::modeWrite|CFile::modeNoTruncate|CFile::typeBinary))
return ;
WORD sign;
wfile.SeekToBegin();
wfile.Read(&sign,2);
if (sign!=0xfeff)
{
AfxMessageBox(L"不是UNICODE文檔");
return;
}
wfile.SeekToEnd();
CString str=L"添加到文檔末尾";
wfile.Write(str.GetBuffer(),str.GetLength()*2);
str.ReleaseBuffer();
wfile.Close();
二 讀文檔:
1 使用Read函數 讀取文檔 ,並將讀取到的內容轉換爲CString 以方便處理
CString filepath;
CStdioFile wfile;
filepath=L"c:\\test.txt";
if (!wfile.Open(filepath,CFile::modeRead|CFile::typeBinary))
{
AfxMessageBox(L"無法打開文件");
return ;
}
ULONGLONG len=wfile.GetLength();
byte *pByte=NULL;
if (len>0)
{
pByte=new byte[len];
memset(pByte,0,len*sizeof(byte));
WORD sign;
wfile.SeekToBegin();
wfile.Read(&sign,2);
if (sign!=0xfeff)
{
AfxMessageBox(L"錄入文件不是Unicode");
}else{
wfile.Read(pByte,len-2);
}
}else{
AfxMessageBox(L"文件長度爲0 ");
return;
}
CString buf=(WCHAR*)pByte; // 將數組轉換爲 CString類型
wfile.Close();
// 其他操作
//...
//
delete [] pByte; // 釋放資源
2 使用 ReadString 讀取一行文本
virtual LPTSTR ReadString( LPTSTR lpsz, UINT nMax ); virtual BOOL ReadString( CString& rString );
// 讀取到換行符\n爲止,且讀取的字符少於nMax-1時,將換行符 \n 保存到緩衝區中
Reading is stopped by the first newline character. If, in that case, fewer than nMax–1 characters have been read, a newline character is stored in the buffer. A null character ('\0') is appended in either case.
注意:
The CString version of this function removes the '\n'
if present; the LPTSTR version does not.
ReadString(CString & rString) 中, rString 爲: 一行文本+結尾符
但結尾是 回車符 '\r' 而不是'\r\n'
因此,若想得到這一行數據,還必須將結尾符去掉方可。
示例:
WORD sign;
readFile.SeekToBegin();
readFile.Read(&sign,2);
if (sign!=0xfeff)
{
AfxMessageBox(L"file.txt 不是Unicode");
}else{
CString str;
//讀取文件
while (readFile.ReadString(str)) // 此時,獲得的str字符串 含有回車符\r 但是不含換行符\n
{
// 去掉 回車符 \r 獲得不含回車換行符的純粹的字符串數據
str=str.Left(str.GetLength()-1);
m_filePathMap.SetAt(str,0);
}
}
一個綜合例子:
CFileDialog dlg(
TRUE,
_T("*"),
_T("*.txt"),
OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,
_T("All Files(*.txt|*.txt||)")
);
TCHAR szbuffer[1024];
szbuffer[0]=0;
dlg.m_ofn.lpstrFile = szbuffer;
dlg.m_ofn.nMaxFile = 1024;
if(IDOK==dlg.DoModal())
{
POSITION pos = dlg.GetStartPosition();
CString filepath;
CStdioFile wfile;
while(pos!=NULL)
{
filepath = dlg.GetNextPathName(pos);
if (!wfile.Open(filepath,CFile::modeReadWrite|CFile::typeBinary))
{
AfxMessageBox(L"無法打開文件");
return ;
}
ULONGLONG len=wfile.GetLength();
CString strTxt;
byte *pByte=NULL;
if (len>0)
{
pByte=new byte[len];
WORD sign;
wfile.SeekToBegin();
wfile.Read(&sign,2);
if (sign!=0xfeff)
{
AfxMessageBox(L"錄入文件不是Unicode");
}else{
pByte[len-1]=0;
pByte[len-2]=0;
wfile.Read(pByte,len-2);
EraseTxtMark(pByte,len);
}
}
CString buf=(WCHAR*)pByte;
wfile.Close();
if (!wfile.Open(filepath,CFile::modeReadWrite|CFile::typeBinary|CFile::modeCreate))
{
AfxMessageBox(L"無法打開文件");
return ;
}
wfile.SeekToBegin();
WORD sign=0xfeff;
wfile.Write(&sign,2);
wfile.Write(buf.GetBuffer(),buf.GetLength()*2);
wfile.Close();
delete [] pByte;
}
}