Summary of C + + File class Learning

Source: Internet
Author: User

For the file operation, I was mainly in contact with Linux under the set, and to C + + learning a bit, but also found some similarities and differences

First file class requires header files # include <stdio.h>

For open file operations, the fopen function is used under Linux, and a warning is reported when VS2010 uses this function

Error C4996: ' fopen ': This function or variable could be unsafe. Consider using fopen_s instead. To disable deprecation, use _crt_secure_no_warnings. See online Help for details.
1> C:\Program Files (x86) \microsoft Visual Studio 12.0\vc\include\stdio.h (211)

VS, I suggest you use fopen_s.

erron_t ret = fopen_s (&file*, "FILENAME", "open Mode");

Open successfully returns 0, the failure returns a value other than 0.

It is worth mentioning that:

File directories in VS need to use \ \ This is not the same as the \ in Linux to distinguish the file level

There's a little bit easier to forget. That is, when the file is opened, the location of the file pointer is related to the open mode, the specific relationship is as follows:

/***************************/

File open with file pointer location

R (read file) file header

W (write file) file header

A (a +) append content file Footer

/***************************/

Files opened with "W" can only be written to the file. If the open file does not exist, the file is created with the specified file name, and if the open file already exists, the file is deleted and a

For file length, the file pointer can be offset to the file header via the rewind () function, and then through Fseek (file*, 0L, seek_end);

How to read and write files

There are fread fwrite fputs fgets and other functions, through these and feof collocation we can realize the detailed operation of reading and writing, see function prototype.

Remember to close the file handle after we're done fclose (file*);

Summary of C + + File class Learning

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.