The usage of CStdioFile is explained in detail

  • 2020-04-02 01:36:12
  • OfStack

About CStdioFile
CStdioFile inherits from CFile, a CStdioFile object that represents a C runtime streaming file opened by the runtime function fopen.

Streaming files are cached and can be opened either as text (by default) or as binary. The text mode provides special handling of hard carriage return - line break pairs. When you write a newline character (0x0A) to a text-based CStdioFile object, the byte pair (0x0D, 0x0A) is sent to the file. When you read a file, the byte pair (0x0D, 0x0A) is translated into a byte (0x0A).

CStdioFile does not support the CFile functions Duplicate, LockRange, and UnlockRange. If these functions are called in the CStdioFile, a CNoSupported exception will occur.
CStdioFile: : ReadString (LPTSTR LPSZ, UINT nMax);

Read a line of text into the buffer, stop reading when "0x0D,0x0A" is encountered, and remove the hard return "0x0D", keep the newline "0x0A", and add "\0" (0x00) at the end of the string. NMax contains the character 0x00.

The analysis is as follows:
1) if nMax < = number of characters, read (nMax-1) characters + 0x00
2) if nMax       = number of characters + 1, read nMax characters + 0x00
3) if nMax       > Number of characters, read nMax characters + 0x0A + 0x00

CStdioFile: : ReadString (cstrings & rString); (heavy)
Read a line of text to the rString, hit enter newline character to stop reading. Return and newline characters do not read to the rString, and "\0" is not added to the end.
CStdioFile: : WriteString (LPCTSTR LPSZ); (CString writing is not supported)

Writes the data in a buffer to the file associated with the CStdioFile object. The ending empty character (" \0 ") is not written to the file. All newlines in LPSZ are written to the file as a hard carriage return newline pair, that is, "\n" is converted to "\r\n" and written to the file.

CStdioFile, a derivative of CFile, provides the ability to stream files. Where the function void CStdioFile::WriteString(LPCTSTR LPSZ) writes a string, the end of the string LPSZ needs to add a newline mark "\r\n"; The function bool CStdioFile::ReadString(CString &rString) reads a line from the file and returns true if the file is not finished, or false if it is not.  

Example: writing to a file


//Create a file
CStdioFile file;
file.Open("ts.txt",CFile::modeCreate|CFile::modeWrite);
//Written to the file
CString str;
str.Format("%srn","hello!I am talkingmute!");
file.Seek(0,CFile::end);
file.WriteString( str );
//Close the file
file.Close();

For example: an example of reading a file

CString strText =  "" ;
CString szLine =  "" ;
//Open the file
CStdioFile file;
file.Open("ts.txt",CFile::modeRead);
//Read the string line by line
while( file.ReadString( szLine ) )
{
strText += szLine;
}
MessageBox(strText);
//Close the file
file.Close();

The declaration of the CStdioFile class is saved in the afx.h header file.
The CStdioFile class does not support the Duplicate, LockRange, and UnlockRange functions in the CFile class, and if you use them, you will get an error in the CNotSupportedException class.
By default, the CStringFile class operates on files in Text mode. By default, the CFile class operates on files in binary mode.

Here is a general explanation of the difference between binary mode and Text mode.
Binary mode:
For the end of a line, we must type "\r\n" to indicate the effect of entering a newline.

The Text mode: "\r" enter is done automatically, we just need to write "\n". So when we use text mode again, when we read the file from the outside, "\r\n" will be translated to "\n", and when we write to the file, we just need to provide "\n" for the return line feed, and "\r\n" will be written to the file.
M_pStream member variable:
Open a pointer to the file.

Constructor:
CStdioFile ();
CStdioFile (FILE * pOpenStream);
CStdioFile (LPCTSTR lpFileName, UINT nOpenFlags);
Throw (CFileException);

FILE *pOpenStream: refers to the FILE pointer returned after c runs the function fopen call.
LPCTSTR lpFileName: refers to the file being opened (absolute or relative address)
UINT nOpenFlags: refers to the way a file is opened as described in the CFile class.

Virtual LPTSTR ReadString(LPTSTR LPSZ, UINT nMax);
Throw (CFileException);

If you use this function to read a text file, when "\r\n" is encountered, stop reading, remove "\r", keep "\n", and increase "\0" at the end of the string.

The actual analysis is as follows:
If nMax < = number of characters, read (nMax-1) characters +0x00;
If nMax = number of characters + 1, read nMax characters +0x00;
If nMax > Number of characters, read nMax characters +0x0A(" \n ") + 0x00;
If the file has multiple lines, NOT NULL is returned when the file is NOT finished, and NULL is returned when the end of the file is read.

BOOL ReadString (cstrings & rString);
Throw (CFileException);

Read a line of text into the rString and stop reading when the carriage return newline character is encountered. Neither the carriage return nor the newline character is read into the rString, and "0x00" is not added to the tail.
If the file has more than one line, it returns TRUE when the file is not finished, and FALSE when the end of the file is read.

Virtual void WriteString (LPTSTR LPSZ);
Throw (CFileException);

Write the data in the buffer to the file associated with the CStdioFile object, no CString type data is written, the ending "\0" is not written to the file, all in the LPSZ buffer


Related articles: