Usage of fopen () and fclose ()

Source: Internet
Author: User

fopen () and the use of fclose ()
1. The use of the fopen () function
The fopen function is used to open a file in a format called:
FILE *fopen (char *filename, *type);
The first form parameter in the fopen () function represents a file name, which can contain both the path and the file name. Such as:
"B:test. DAT "
"C:\\tc\\test. DAT "
Note: If the path is written as "C:\TC\TEST." DAT "is incorrect, pay special attention to this point.
The fopen function is used to open a file whose general form of invocation is: file pointer name =fopen (filename, using file mode)
Where the "file pointer name" must be a pointer variable that is described as a file type, "file name" is the file name of the files being opened.
"Use file Mode" refers to the type of file and the operational requirements. "File name" is a string constant or an array of strings. For example:
FILE *FP;
fp= ("File A", "R");
The meaning is to open file A in the current directory, only allow "read" operation, and make FP point to the file.
Another example:
FILE *FPHZK
fphzk= ("C:\\hzk16", "RB")
The significance is to open the C drive disk root directory of the file hzk16, which is a binary file, only allowed to read in binary mode.
The first of the two backslashes "\ \" represents an escape character, and the second represents the root directory. There are 12 ways to use the file, and the symbols and meanings are given below.
The second form parameter represents the type of open file. See the table below for provisions on file types.








Table file operation type
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Character meaning
────────────────────────────
"R" Open text file read only
"W" creates a text file that only writes
The "A" supplement, if the file does not exist, creates a
"R+" opens a text file read/write
"w+" Create a text file read/write
"A +" opens or creates a file supplement
"B" binaries (can be combined with each of the above)
"T" this file (default)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Meaning of the way the file is used
"RT" read-only open a text file, allowing only read data
"WT" only writes Open or create a text file, allowing only write data
"At" Appends open a text file and writes data at the end of the file
"RB" read-only open a binary file, allowing only read data
"WB" only writes open or create a binary file, allowing only write data
"AB" appends open a binary file and writes data at the end of the file
"rt+" read and write open a text file that allows read and write
"wt+" read/write open or create a text file that allows read and write
"at+" read and write open a text file, allow reading, or append data at the end of the file
"rb+" read and write open a binary file that allows read and write
"wb+" read-write open or create a binary file that allows read and write
"ab+" read and write open a binary file, allow reading, or append data at the end of the file

There are several explanations for how the file is used:
1. File usage by r,w,a,t,b,+ Six characters, the meaning of each character is:
R (Read): Read
W (write): Write
A (append): Append
T (text): Literal file, can be omitted without writing
B (banary): binary file
+: Read and Write

2. When a file is opened with "R", the file must already exist and can only be read from the file.

3. 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 new file is rebuilt.

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 will return a null pointer value of NULL. In the program can use this information to determine whether to complete the work of open files, and corresponding processing. 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 empty, indicating that the hzk16 file under the C packing directory cannot be opened, the message "Error on open C: \ hzk16file!" is given, and the next line of Getch () is to enter a character from the keyboard but not on the screen. Here, the function of the line is to wait, and the program will continue to execute only when the user taps any key from the keyboard, so the user can use this wait time to read the error prompt. Execute exit (1) to exit the program after tapping the key.

6. When a text file is read into memory, the ASCII code is converted to binary code, and the file is written in text to the disk, the binary code is converted to ASCII code, so the text file read and write to spend more time to convert. There is no such conversion to read and write binary files.

7. Standard input file (keyboard), standard output file (display), standard error output (Error message) is open by the system, can be used directly. File Close function fclose file once used, the closed file function is applied to close the file to avoid errors such as data loss of the file.









To open a Ccdos subdirectory, a binary file named Clib can be written as:
fopen ("C:\\ccdos\\clib", "RB");
If a file is successfully opened, the fopen () function returns the file pointer, otherwise a NULL pointer (NULL) is returned. This will determine whether the file opened successfully.
2. Fclose () function
The fclose () function is used to close a file opened by the fopen () function, which is called in the format:
NT fclose (FILE *stream);
Example:
FILE *fpout=fopen ("C:\\a.txt", "wt+");
Int a=1;
fprintf (Fpout, "%d", a);
Fclose (fpout);

Usage of fopen () and fclose ()

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.