Visual c ++ File Operations

Source: Internet
Author: User

The cfile class allows you to open, close, read/write, delete, rename, and obtain file information.
The basic functions are sufficient to process any types of file operations.
The cfile class is the base class of the MFC File class. It supports binary input and output without buffering. It can also be used with the carchive class
To support the serialization of the MFC object with buffer.
You can use the cfile class to open a file in two ways: one is to first construct a cfile class object and then call
The member function open () opens the file,
Another method is to directly use the cfile class constructor to open a file. The following statement demonstrates the use of the two
Method to open the disk file "C: \ testfile.txt.

1. Create an instance and then open the file
Cfile file;
Int ret = file. Open ("C: \ testfile.txt", cfile: modereadwrite );
// Whether the report file is successfully opened
If (ret = true)
Afxmessagebox ("file opened successfully! ");
Else
Afxmessagebox ("file opening failed! ");
2. open the file directly through the constructor
Cfile file ("C :\\ testfile.txt", cfile: modereadwrite );

The cfile: modereadwrite parameter indicates the mode in which the file is opened. There are also 10 marks in the cfile class and the like.
A few, first concentrated columns as shown in the table below:

Table 1 file mode flag
── ─
File mode flag description
── ─
Cfile: modecreateOpen the file in creation mode. If the file already exists, set its length to 0.
── ─
Cfile: modenoinheritInheritance Not Allowed
── ─
Cfile: modenotruncateWhen creating a file, if the file already exists, it will not be truncated.
── ─
Cfile: modereadOpen a file in read-only mode
── ─
Cfile: modereadwriteOpen a file in read/write mode
── ─
Cfile: modewriteOpen a file by writing
── ─
Cfile: compatAllow other processes to open files simultaneously during use
── ─
Cfile: sharedenynoneAllow other processes to read and write files during use
── ─
Cfile: sharedenyreadOther processes are not allowed to read and write files during use.
── ─
Cfile: sharedenywriteOther processes are not allowed to write files during use.
── ─
Cfile: Export exclusiveCancel all access to other processes
── ─
Cfile: typebinarySet file to binary mode
── ─
Cfile: typetextSet file to text mode
── ─

These signs can be used simultaneously through the "or" operator to meet multiple requirements.
For example:
Cfile file ("C :\\ testfile.txt", cfile: modewrite | cfile: modecreate );

You must disable an opened file when it is no longer in use. You can use the member function close () or
Cfile class destructor. When the last method is used, if the file is not closed, the Destructor will
Call the close () function implicitly to close the file. This also indicates that the cfile class object created on the stack is out of range.
And will be disabled automatically. Because the object destructor are called, The cfile object is also
And close (), the cfile object still exists. Therefore, when you explicitly call close ()
After the function closes a file, you can continue to use the same cfile object to open other files.

File read/write is the most common file operation method. It is mainly implemented by the cfile member functions read () and write.
The function prototype is as follows:
Uint read (void * lpbuf, uint ncount );
Void write (const void * lpbuf, uint ncount );
The lpbuf parameter is the pointer to the cache where data is stored, and ncount is the number of bytes to be read or written. Read () returns
Is the actual number of bytes read. This value is less than or equal to ncount. If it is less than ncount, it indicates that the file has been read.
You can end reading the file. If you continue reading the file, 0 is returned. Therefore, the actual number of bytes read is usually small.
The specified number of bytes to read or whether it is 0 is used as the basis for determining whether the File Read ends. The following code demonstrates
Processing of one-time writing and multiple-times reading of files.

// Cache
Char writebuf [1000];
Char readbuf [100];

// Create and write a file to open it
Cfile file;
File. Open ("C :\\ testfile.txt", cfile: modewrite | cfile: modecreate );

// Write a file
Memset (writebuf, 'A', sizeof (writebuf ));
File. Write (writebuf, sizeof (writebuf ));

File. Close ();

// Open the file in read-only mode
File. Open ("C: \ testfile.txt", cfile: moderead );

While (true)
{
// Read file data
Int ret = file. Read (readbuf, 100 );
......
// Stop the loop if it reaches the end of the file
If (Ret <100)
Break;
} // While

// Close the file
File. Close ();

After the write () and read () functions are executed, the file pointer is automatically moved, so you do not need to explicitly call the seek () function.
Locate the file pointer. The complete code containing the file locating function is as follows:

Char writebuf [1000];
Char readbuf [100];

Cfile file;
File. Open ("C :\\ testfile.txt", cfile: modewrite | cfile: modecreate );
// Write a file
Memset (writebuf, 'A', sizeof (writebuf ));
File. seektobegin ();
File. Write (writebuf, sizeof (writebuf ));

File. Close ();

File. Open ("C: \ testfile.txt", cfile: moderead );

While (true)
{
// File pointer
Static int position = 0;
// Move the file pointer
File. Seek (Position, cfile: Begin );
// Read file data
Int ret = file. Read (readbuf, 100 );
Position + = ret;
......
// Stop the loop if it reaches the end of the file
If (Ret <100)
Break;
} // While

File. Close ();

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.