Usage of fopen () and fclose ()

Source: Internet
Author: User
Fopen ()And fclose () Usage
1. usage of the fopen () function
The fopen function is used to open a file. The call format is:
FILE *fopen(char *filename, *type);
The first form parameter in the fopen () function represents the file name, which can contain two parts: path and file name. For example:
"B:TEST.DAT"
"C:\\TC\\TEST.DAT"
Note: It is incorrect to write the path "C: \ TC \ test. dat.
The fopen function is used to open a file. Generally, it is called in the form of: file pointer name = fopen (file name, file method)
The "file pointer name" must be a pointer variable described as file type, and the "file name" is the file name of the opened file.
"File Usage" refers to the file type and operation requirements. The "file name" is a String constant or a string array. For example:
File * FP;
Fp = ("file a", "R ");
It means to open file a in the current directory, only allow "read" operations, and direct FP to the file.
Another example:
File * fphzk
Fphzk = ("C: \ hzk16'," rb ")
It means to open the file hzk16 under the root directory of drive C, which is a binary file and only allows read operations in binary mode.
The first character in the two Backslash "\" represents the escape character, and the second is the root directory. There are 12 methods to use files. Their symbols and meanings are given below.
The second form parameter indicates the type of the file to open. For more information about file types, see the following table.








Table File Operation Type
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Character meaning
    ────────────────────────────
"R" Open Text File Read-Only
"W" Create a text file and write only
"A" addition. If the file does not exist, create
"R +" open a text file to read/write
"W +" Create a text file read/write
"A +" open or create a file supplement
"B" binary file (can be used with each of the above items)
"T" file (default)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
File Usage
"RT" only opens a text file and only supports reading data.
"WT" only writes to open or create a text file, only data can be written
Append "at" to open a text file and write data at the end of the file.
"Rb" is used to open a binary file and only read data.
"WB" only writes to open or create a binary file, only data can be written
Append "AB" to open a binary file and write data at the end of the file.
"RT +" reads and writes to open a text file, allowing reading and writing
"WT +" reads and writes to open or create a text file, allowing reading and writing
"At +" to open a text file, allow reading, or append data to the end of the file
"RB +" reads and writes a binary file, allowing reading and writing
"WB +" to open or create a binary file and allow reading and writing
"AB +" reads and writes to open a binary file, allowing reading or appending data at the end of the file

The usage of files is described as follows:
1. The file is comprised of R, W, A, T, B, and +. The meanings of each character are as follows:
R (read): Read
W (write): Write
A (append): append
T (text): Text File, can be omitted without writing
B (banary): Binary File
+: Read and Write

2. When you use "R" to open a file, the file must already exist and can only be read from the file.

3. A file opened with "W" can only be written to this file. If the opened file does not exist, the file is created with the specified file name. If the opened file already exists, delete the file and create a new file.

4. to append new information to an existing file, you can only open the file in "A" mode.
5. If an error occurs when opening a file, fopen returns a null pointer value. In the program, this information can be used to determine whether to complete the file opening and handle it accordingly. Therefore, the following sections are commonly used to open files:
If (FP = fopen ("C: \ hzk16", "rb") = NULL)
{
Printf ("\ nerror on Open c :\\ hzk16 file! ");
Getch ();
Exit (1 );
}
The meaning of this program is that if the returned pointer is null, it indicates that the hzk16 file under the root directory of the C drive cannot be opened, the message "error on Open c: \ hzk16file!" is displayed !", The next line of getch () is used to input a character from the keyboard, but is not displayed on the screen. Here, the role of this row is to wait. The program continues to be executed only when you press one key from the keyboard. Therefore, you can use this wait time to read the error message. Press the key and execute exit (1) to exit the program.

6. when reading a text file into the memory, you need to convert the ASCII code into a binary code. When writing the file into a disk as text, you also need to convert the binary code into an ascii code, therefore, it takes a lot of time to read and write text files. This conversion does not exist for reading/writing binary files.

7. The standard input file (keyboard), standard output file (Display), and standard error output (error information) are opened by the system and can be used directly. Once the fclose file function is used, the application closes the file function to avoid errors such as file data loss.









To open a binary file named cLib in the ccdos subdirectory, you can write it as follows:
fopen("c:\\ccdos\\clib", "rb");
If a file is successfully opened, the fopen () function returns the file pointer; otherwise, null is returned ). This allows you to determine whether the file is successfully opened.
2. fclose () function
The fclose () function is used to close a file opened by the fopen () function. The Calling format is:
   nt fclose(FILE *stream);
This function returns an integer. If the object is successfully closed, 0 is returned; otherwise, a non-zero value is returned. You can determine whether the file is successfully closed based on the return value of the function.
Example:
FILE *fpOut=fopen(“c:\\a.txt”,”wt+”);
Int a=1;
Fprintf(fpOut,”%d”,a);
Fclose(fpOut);

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.