CStdioFile usage Detailed parsing _c language

Source: Internet
Author: User

About CStdioFile
CStdioFile inherits from CFile, and a CStdioFile object represents a C run Shireu file that is opened with a run-time function fopen.

Streaming files are buffered and can be opened in either text (default) or binary mode. Text provides special handling of hard return-line feed pairs. When you write a newline character (0x0A) to a text-mode CStdioFile object, the byte pair (0x0d,0x0a) is sent to the file. When you read a file, the byte pair (0x0d,0x0a) is translated to a byte (0x0A).

CStdioFile does not support Duplicate,lockrange, and unlockrange these several cfile functions. If these functions are called in CStdioFile, a cnosupported exception will appear.
Cstdiofile::readstring (LPTSTR lpsz, UINT Nmax);

Reads a line of text to the buffer, stops reading when "0x0d,0x0a" is encountered, removes the hard return "0x0D", preserves the newline character "0x0A", and Adds "a" (0x00) at the end of the string. The Nmax character character contains the 0x00.

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

Cstdiofile::readstring (CString &rstring); (overload)
Reads a line of text to rstring and encounters a carriage return newline character to stop reading. The carriage return and newline characters are not read to the rstring and the end is not added.
Cstdiofile::writestring (LPCTSTR lpsz); (Does not support direct write to CString)

Writes data from one buffer to the file associated with the CStdioFile object. The ending null character ("") is not written to the file. All newline characters in the lpsz are written to the file in a hard return newline, which means "\ n" is converted to "\ r \ n" and written to the file.

The CFile derived class CStdioFile provides a streaming operation of the file. where the function void cstdiofile::writestring (LPCTSTR lpsz) writes a string, the end of the string lpsz is added with a newline flag "\ r \ n"; function bool Cstdiofile::readstring ( CString &rstring) reads a row from a file and returns False if the file returns true if it is not read out.

For example: writing to a file

Copy Code code as follows:

Create a file
CStdioFile file;
File. Open ("Ts.txt", cfile::modecreate| Cfile::modewrite);
Write to File
CString str;
Str. Format ("%s\r\n", "hello! I am talkingmute! ");
File. Seek (0,cfile::end);
File. WriteString (str);
Close File
File. Close ();

For example: Read the file examples
Copy Code code as follows:

CString strText = "";
CString szline = "";
Open File
CStdioFile file;
File. Open ("Ts.txt", Cfile::moderead);
Read string by line
while (file. ReadString (Szline))
{
StrText + = Szline;
}
MessageBox (StrText);
Close File
File. Close ();

The declaration of the CStdioFile class is saved and afx.h the header file.
The CStdioFile class does not support the duplicate, LockRange, UnlockRange functions in the CFile class, and if you use it, you get the CNotSupportedException class error.
The Cstringfile class defaults to manipulating files in text mode. The CFile class defaults to manipulating files in binary mode.

Here is a general description of the difference between binary mode and text mode.
binary mode:
for the end of a line, we must enter "\ r \ n" to indicate the effect of the carriage return line change.

text mode:"\ r" return work is done automatically, we only need to write "\ n" can be. So when we use the text pattern again, the main, when we read the file from the outside, "\ r \ n" will be translated to "\ n", when we write to the file, we only provide "\ n" for the carriage return line, "\ r \ n" will be written to the file.
M_pstream member Variable:
The pointer to open the file.

constructor function:
CStdioFile ();
CStdioFile (FILE *popenstream);
CStdioFile (LPCTSTR lpfilename, UINT nopenflags);
throw (CFileException);

File *popenstream: refers to the document pointer returned after the C run function fopen call.
LPCTSTR lpFileName: refers to an open file (an absolute or relative address)
UINT nopenflags: refers to the way the 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 you encounter "\ r \ n", stop reading, and remove "\ r", leave "\ n", and add "Nmax" at the end of the string, and the length of the section contains the "Yes" character,

The

actual analysis is as follows:
if Nmax <= characters, read (nMax-1) characters +0x00,
if Nmax = character number + 1, read Nmax characters +0x00
If the Nmax > characters are read, the Nmax character +0x0a ("\ n") + 0x00;
If the file has more than one line, returns NOT NULL when the file is not read, reads to the end of the file, and returns NULL.

BOOL ReadString (cstring& rstring);
throw (CFileException);

Reads a line of text into rstring, encounters a carriage return line break, stops reading, a carriage return and a newline character are not read into the rstring, and the tail does not add "0x00."
If the file has more than one row, returns True when the file is not read, and returns false after reading to the end of the file.

virtual void writestring (LPTSTR lpsz);
throw (CFileException); The

writes data in the buffer to the file associated with the CStdioFile object, does not support CString type data writes, and the ending "" is not written to the file, and all

in the lpsz buffer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.