Open files and close files

Source: Internet
Author: User
Tags first string

1 Open File

Before reading or writing to the file, the first problem is how to connect the files that are read and written in the program to the actual data files on the disk. In the C language, this is not difficult, just call the C-language library function fopen "open" file to achieve these connections. The general invocation form of the fopen function is:

fopen (file name, how to use it);

The function returns a pointer to the FILE type. For example:

FILE *FP;

fp = fopen ("File_a", "R");

There are two strings in the fopen function call as arguments. The first string contains the file name for the read, write operation to specify the file to open. In this example, the specified function is named: File_a. The second string specifies how the file is used, which allows the user to specify the intent to use the file.

If the above function call succeeds, the function returns a pointer to the file type, assigns the filename to the pointer variable FP, thereby linking the pointer FP to the file file_a. That is, after this call, the pointer FP points to the file file_a. When an error occurred while opening the file, the fopen function returns NULL. To ensure that the correct open files are used in your program, the following sections are recommended. When an error occurs in an open file, the program stops running:

#include <stdlib.h>

...

if (PF = fopen ("File_a", "r")) = = = NULL)

{

printf ("Cannot open this file!\n");

Exit (0); Note: When you use the Exit function, you must include the Stdlib.h header file

}

C language, the most common use of the file and its meaning is as follows:

(1) "R". Open a text file for reading. When this is specified, only the "read" operation is performed on the open file. An error occurs if the specified file does not exist, and in some cases an error occurs when attempting to read a file that is not allowed to read.

(2) "RB". Opens a binary file for reading. The remaining features are the same as "R".

(3) "W". Open a text file for writing. If the specified file does not exist, a new file is created with the file name specified in the fopen call, and if the specified file already exists, it will be written from the beginning of the file and the contents of the file will all disappear.

(4) "WB". Opens a binary file for writing. You can write at the specified file location, and the remaining features are similar to "w".

(5) "a". Open a text file to add data to the back of the file. If the specified file does not exist, a new file is created with the file name specified in the fopen call, and if the specified file already exists, the contents of the file are saved and the new data is written after the original content.

(6) "AB". Opens a binary file to add data to the back of the file. The remaining features are the same as "a".

(7) "r+". Open a text file for reading and writing. In this way, the specified file should already exist, either to read the file or to write the file, without closing the file between the read and write operations. Just for text files, read and write always start at the beginning of the file. When writing new data, only the space occupied by the new data is overwritten, and the old data behind it is not lost.

(8) "rb+". Open a binary file for reading and writing. The function is the same as "r+", but when reading and writing, the position function can be used to set the starting position of the read and write, that is, not necessarily starting from the beginning of the file to read and write.

(9) "w+". Start by creating a new file, write it, and then read from scratch. If the specified file already exists, the original content disappears.

(Ten) "wb+". The function is the same as "w+", but the starting position of the read and write can be set by the position function when the subsequent read and write.

(one) "A +". The function is the same as "a", but after adding new data at the end of the file, you can read from the beginning.

(a) "ab+". The function is the same as "A +", but after adding new data at the end of the file, you can set the starting position of the start read by the location function.

When running a C program, the system will be responsible for automatically opening three files, the three files are standard input files, standard output files and standard error files, and the corresponding file pointers are stdin, Sdtout, stderr, which are described in the Stdio.h header file. Typically, stdin is connected to the terminal screen with keyboard connections, stdout, and stderr. Note: These pointers are constants, not variables, and therefore cannot be re-assigned.

2 Closing files

When a read (write) operation on a file is completed, it must be closed. Closing the file can call the library function fclose implementation, the Fclose function is called the following form:

fclose (file pointer);

If the FP is a file pointer to the file file_a, when the fclose (FP) is executed; After that, if the file file_a is "read", then the file pointer fp will be disconnected from the file file_a. After this, the file pointer FP can be reassigned to point to other files. If the file file_a operation is "write" mode, then the system first put the remaining data in the file buffer to the file, and then the file pointer fp and the file file_a out of contact. This shows that after you complete the operation of the file, you should close the file, or the remaining data in the file buffer will be lost.

When the close operation is successfully performed, the function returns 0, otherwise it returns non 0.

Open files and close files

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.