How to read and write text files in behavior units

xiaoxiao2021-03-06  51

In actual work, we often need to operate text files in behavioral units, such as reading and writing

ini

Document, etc. If we use

C

or

C

The function in the runtime library is a little more troublesome. If you use

MFC

Use if you are doing

Cstdiofile

This class.

Let's take a look at CSTDIOFILE:

Class Cstdiofile: Public Cfile.

It is inherited from the CFILE class. In addition to constructor, it only provides two functions:

Virtual LPTSTR READSTRING (LPTSTSTSTSZZ, UINT NMAX); a line of reading text.

Virtual Bool ReadString (CString & Rstring); Write a line of text.

Cstdiofile hfile;

CfileException EX;

Bool ISEOF = FALSE;

IF (! hfile.open (m_hfilepath, cfile :: moderad | cfile :: sharedenywrite, & ex)) // Open file

{

Tchar Szerror [1024];

EX.GETERRORMESSAGE (Szerror, 1024);

AfxMessageBox (Szerror);

Return;

Else

{

While (! ISEOF)

{

ISEOF =! hfile.readstring (strtemp); // reads a line of text.

ISEOF BREAK;

...... DO Something

}

}

CString stratmp;

StrtMP = "Write a line of text / N";

Hfile.writeString (StrtMP);

转载请注明原文地址:https://www.9cbs.com/read-116713.html

New Post(0)