C language File Open and close detailed and sample code _c language

Source: Internet
Author: User

In C, file operations are done by the library function, which describes the opening and closing of files.

File open (fopen function)

The fopen () function is used to open a file whose prototype is:

FILE *fopen (Char *filename, char *mode);

FileName is the filename (including the file path), mode is open, and they are all strings. fopen () Gets the file information, including the file name, file status, current read-write location, and so on, saves the information to a struct variable of type file, and then returns the address of the variable.

File is a struct that is defined in the Stdio.h header file, which is used to hold information about the files.

If you want to receive the return value of fopen (), you need to define a pointer to a FILE type. For example:

FILE *FP = ("Demo.txt", "R");

Indicates that the Demo.txt file in the current directory is opened in read-only mode and the FP is directed to the file, so that the FP can be used to manipulate the demo.txt. FP is usually

Called a file pointer. Another example:

FILE *FP = fopen ("D:\\demo.txt", "RB");

Indicates that the Demo.txt file under D disk is opened in binary mode, allowing read and write.

There are several ways to open (mode), as shown in the following table:

Open Mode Description
R Opens the file as read-only, allowing only reading and not writing. The file must exist.
r+ Opens the file in read/write mode, allowing read and write. The file must exist.
rb+ Open a binary file in read/write mode that allows you to read/write data.
rt+ Open a text file in read/write mode, allowing read and write.
W Open the file as write-only, if the file exists, the length is 0, the contents of the file disappear, and the file is created if it does not exist.
w+ Open the file as read/write, if the file exists, the file length is zero, that is, the contents of the file will disappear. If the file does not exist, the file is created.
A Open a write-only file as an append. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, i.e. the original contents of the file will be retained (the EOF character is retained).
A + Open a readable/writable file as an append. If the file does not exist, the file will be created, and if the file exists, the written data will be added to the end of the file, i.e. the original contents of the file will be retained (the original EOF character is not retained).
Wb Open or create a new binary file in write-only mode, allowing only data to be written.
wb+ Open or create a binary file in read/write mode, allowing read and write.
wt+ Open or create a text file in read/write mode that allows reading and writing.
at+ Opens a text file in read/write mode that allows reading or appending data at the end of the text.
ab+ Opens a binary file in read/write mode that allows you to read or append data at the end of the file.

Differences between text files and binaries see: C language fopen () the difference between opening a text file and a binary file

A few notes

1 File Open Way by R, W, A, T, B, + six characters spelled, the meaning of each character is:

    1. R (Read): Read
    2. W (write): Writing
    3. A (append): Append
    4. T (text): text file, can omit not write
    5. B (banary): Binary files
    6. +: Read and Write

2 If there is no "B" character, the file is opened in text mode.

3 When a file is opened with "R", the file must already exist.

4 When a file is opened, if an error occurs, FOPEN returns a null pointer value. In the program can use this information to determine whether to complete the work of opening the file, and the corresponding processing. Therefore, the following program segments are commonly used to open files:

if ((Fp=fopen ("D:\\demo.txt", "rb") = = NULL) {
  printf ("Error on Open d:\\demo.txt file!");
  Getch ();
  Exit (1);
}

The meaning of this program is that if the returned pointer is empty, the Demo.txt file in the D-packing directory cannot be opened, and the prompt message "error on open d:\\demo.txt file!" is indicated. The function of line 3rd Getch () is to enter a character from the keyboard, but not on the screen. Here, the function of the row is to wait, and the program continues to execute only when the user knocks any key from the keyboard, so the user can use the wait time to read the error message. After hitting the key, execute exit (1) to exit the program.

5 When a text file is read into memory, the ASCII code is converted into binary code, and when the file is written to disk, the binary code is converted into ASCII code, so the reading and writing of the text file will cost more conversion time. This conversion does not exist for reading and writing to binary files.

6 standard input file stdin (keyboard), standard output file stdout (monitor), standard error file stderr (monitor) is opened by the system, can be used directly.

File shutdown (fclose function)

Once the file has been used, you should use the fclose () function to close the file to release the relevant resources to avoid data loss. The prototype for fclose () is:

int fclose (FILE *fp);

FP is a file pointer. For example:

Fclose (FP);

When the file shuts down normally, the return value of fclose () is 0, and if a non-0 value is returned, an error occurs.

The above is the document of the basic information on the detailed, follow-up to continue to supplement the relevant information, thank you for your support of this site!

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.