Transfer from http://blog.csdn.net/shuilan0066/article/details/6996310
One: Write a document
1 Creating a document and writing content
CString filepath=l "C:\\unicode.txt";
CStdioFile Wfile;
if (!wfile. Open (filepath,cfile::modecreate| cfile::modewrite| Cfile::typebinary))
{
AfxMessageBox (L "file cannot be modified");
Return
}
WORD Sign=0xfeff; Unicode Document Flags
Wfile. Write (&sign,2);
CString filecontent;
Filecontent=l "Unicode Document";
Wfile. Write (Filecontent.getbuffer (), Filecontent.getlength () * *);
Filecontent.releasebuffer ();
Wfile. Close ();
-----------------------------------
WORD Sign=0xfeff;
Wfile.write (&sign,2);
Traversing the list container to write to a file
List<cstring>::iterator It=filelist.begin ();
For (It=filelist.begin (); It!=filelist.end (); it++)
{
Str=*it;
Writes a string of type CString
Wfile.write (str. GetBuffer (), str. GetLength () * *);
Str. ReleaseBuffer ();
Write carriage return line break with WriteString
Wfile.writestring (L "\ r \ n");
}
Wfile.close ();
2 Determine if the document exists, if present, add content at the end of the document, if it does not exist, recreate
CString filepath=l "C:\\Test.txt";
CStdioFile Wfile;
CFileFind FileFind;
if (!filefind.findfile (FilePath)//Find file exists
{
if (!wfile. Open (filepath,cfile::modewrite| cfile::modecreate| cfile::typebinary))//unicode must be opened in binary
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 "is not a Unicode document");
Return
}
Wfile. Seektoend ();
CString str=l "Add to end of document";
Wfile. Write (str. GetBuffer (), str. GetLength () * *);
Str. ReleaseBuffer ();
Wfile. Close ();
Second-reading documents:
1 Use the Read function to read the document and convert the read to CString for easy handling
CString filepath;
CStdioFile Wfile;
Filepath=l "C:\\Test.txt";
if (!wfile. Open (filepath,cfile::moderead| Cfile::typebinary))
{
AfxMessageBox (L "Can't open File");
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 "input file is not Unicode");
}else{
Wfile. Read (pbyte,len-2);
}
}else{
AfxMessageBox (L "file length is 0");
Return
}
CString buf= (wchar*) pbyte; Convert an array to a CString type
Wfile. Close ();
Other operations
//...
//
delete [] pbyte; Freeing resources
2 reading a line of text using ReadString
virtual LPTSTR ReadString ( LPTSTRlpsz, UINT); virtual BOOL ReadString ( rString);
The newline character \ n is saved to the buffer when it is read to \ n, and the characters read are less than nMax-1
Reading is stopped by the first newline character. If, in this case, fewer than nmax–1 characters has been read, a newline character is stored in the buffer. A null character (' appended ') is the in-either case.
Attention:
The CString version of this function removes the ‘\n‘ if present, the LPTSTR version does not.
ReadString (CString & rString), rString is: one line of text + end character
But the end is the carriage return ' \ R ' instead of ' \ r \ n '
Therefore, if you want to get this line of data, you must also remove the end character.
Example:
WORD sign;
Readfile.seektobegin ();
Readfile.read (&sign,2);
if (Sign!=0xfeff)
{
AfxMessageBox (L "file.txt is not Unicode");
}else{
CString str;
Read file
while (readfile.readstring (str))//At this time, the obtained STR string contains a carriage return \ r but does not contain a newline character \ n
{
Remove carriage return \ r Gets the pure string data without carriage return line feed
Str=str. Left (str. GetLength ()-1);
M_filepathmap.setat (str,0);
}
}
A comprehensive example:
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 "Can't open File");
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 "input file is not 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 "Can't open File");
return;
}
Wfile. Seektobegin ();
WORD Sign=0xfeff;
Wfile. Write (&sign,2);
Wfile. Write (BUF. GetBuffer (), buf. GetLength () * *);
Wfile. Close ();
delete [] pbyte;
}
}
Read and write Unicode documents using CStdioFile