MFC summary (a): MFC file operations, locate and select the summary (1)

       This paper mainly involves the following aspects, are more commonly used in the MFC programming: CFile operations, CArchive operation, how to select files and folders, how to find a file, get the current directory, _access function and so on.

     (A) Operation class file using CFile

       MFC CFile class in the file operation should be the most useful one. The main member functions Open, Close, Write, Read, Seek, SeekToEnd, GetLength, GetPosition, specific usage see MSDN.

       (1) file open operation:

        Open the file in two ways, one is to define the class object, opened with the Open function; one is the name of the incoming file and open way when defining the class object. They are as follows

        CFile file;

        if(!file.Open(lpszFileNamenOpenFlags ))

              return;

       or

       CFile file(lpszFileNamenOpenFlags);

      (2) Open the file:

       Open file, i.e. above nOpenFlags value is primarily a combination of some of the values from macro definitions, see the MSDN particular, commonly used are the following:

       A. CFile :: modeCreateCFile :: modeNoTruncate | CFile :: modeWrite , to write the file opened, if the file does not exist, create a new file, or open an existing file for writing operation, this time does not clear the original documents ;

       B. The above CFile :: modeWrite replaced CFile :: modeRead or CFile :: modeReadWrite may be implemented simultaneously read or write.

    (3) determine the end of file

     Determine whether the end of the file, not finished continue reading, otherwise close the exit. This is commonly used in programming. A method for determining the end of the file is commonly used CFile:

    while(file.GetPosition() < file.GetLength())

    {// file is not the end

           //Add code ....

    }

    (4) It is worth noting that:

     A. When the file name is included in the path, to be noted that the use \\ , such as C: \\ myfile.dat , while at the Unicode, need _T ( "C: \\ myfile.dat") ;

     B. using (2) A way to write the file, note the use of file.SeekToEnd () will move the file pointer end of the file, and then write, otherwise it will overwrite the existing data, this point is often overlooked many beginners;

     C. After opening the file will immediately write down Close, get in the habit, avoid forget to close the file. In particular, for a data read to judge how qualify the function returns the file is closed before then we must remember to return;

     D. CFile in bytes to read and write files are achieved.

        Unfinished, continued. . . .

            

Published 37 original articles · won praise 204 · views 440 000 +

Guess you like

Origin blog.csdn.net/zwgdft/article/details/7104767