ReadTextFile Method
static bool CMarkup::ReadTextFile(
MCD_CSTR szFileName,
MCD_STR& strDoc,
MCD_STR* pstrError=NULL,
int* pnFlags=NULL
);
ReadTextFile is a static function (with no knowledge of the data members of a CMarkup object) used internally by the Load method. It reads a file into a string, sometimes performing a text encoding conversion. See ANSI and Unicode Files.
Sometimes you need to know whether a failure to load is due to a file error, or a parser error. In this case, using the Load method can be inconvenient as described in When CMarkup Load Returns false. You can implement your own code to read the file, and then pass the string to SetDoc; however, you might prefer not to have to implement your own file reading code, especially when there are text encoding issues. This sample code displays separate error messages if the file read fails, or the document parse fails:
CMarkup xml;
CString csDoc, csResult;
if ( ! CMarkup::ReadTextFile("file.xml",csDoc,&csResult) )
AfxMessageBox( "File Error: " + csResult );
else if ( ! xml.SetDoc(csDoc) )
AfxMessageBox( "Parse Error: " + xml.GetError() );
The ReadTextFile function returns true if the text file is successfully loaded into the document string argument. If the optional result string argument is supplied, it is set to the error string on failure, and the number of bytes read on success.
The last argument to the ReadTextFile method is the flags. This is an optional argument; it will not change the way a file is read. ReadTextFile will set the MDF_UTF16LEFILE or MDF_UTF8PREAMBLE flag if the corresponding BOM is encountered (BOMs are removed from the text). If you are going to write the same file back to disk after modifying it, it is best to get the flags from ReadTextFile and keep them to pass them to WriteTextFile so the file is saved the same way it is loaded.
For more on these flags, see UTF-8 Files and the Preamble and UTF-16 Files and the Byte Order Mark (BOM).
|