The basic course of C language (iii) input and output functions and control flow statements (8)
Source: Internet
Author: User
1.fopen () function
The fopen function is used to open the file, which is called in the format: File *fopen (char *filename, *type);
Before introducing this function, let's take a look at the following knowledge.
(1) flow (stream) and files (file)
Streams and files are different in Turbo C2.0, and Turbo C2.0 provides a layer of abstraction between the programmer and the accessed device, called "streaming", and the actual device is called a file. A stream is a logical device that has the same behavior. Therefore, the function used to write the disk file can also be used to write the printer. There are two types of streams in the Turbo C2.0: text stream and binary (binary stream). is a text file and binary file for disk. The software makes it easy for readers to understand the Turbo C2.0 language without special distinction between convection and file.
(2) Document pointer file
The file is actually a new data type. It is a collection of the basic data types of the turbo C2.0, called the structure pointer. The concept of structs will be described in detail in section fourth, where file is understood as a data structure that includes information about file management, that is, you must first define a file pointer when you open it.
(3) The function call format described later will directly write out the data type of the formal parameter and the data type of the function return value. For example, the function that opens the file above returns a file pointer with two formal arguments, both character variables (string arrays or string pointers). The software no longer describes the calling format of the function in detail.
Now let's look at the use of open file functions.
The first form parameter in the fopen () function represents the file name and can contain both the path and the file name. Such as:
"B:test. DAT "
"C:\\tc\\test. DAT "
If the path is written as "C:\TC\TEST." DAT "is not correct, this point should pay special attention to.
The second form parameter represents the type of open file. The requirements for file types are shown in the following table.
Table File Action type
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Character meaning
────────────────────────────
"R" opens text file read only
"W" creates a text file that writes only
"A" supplement, create a file if it does not exist
"R+" opens a text file read/write
"w+" creates a text file read/write
"A +" opens or creates a file supplement
"B" binaries (can be shared with each of the above)
"T" text this file (default)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
If you want to open a Ccdos subdirectory, the file name is a clib binary file that can be written as:
fopen ("C:\\ccdos\\clib", "RB");
If a file is successfully opened, the fopen () function returns the file pointer, otherwise null (NULL). This allows you to determine whether the file was opened successfully. 2. Fclose () function
The fclose () function is used to close a file opened by the fopen () function, which is called in the following format:
int fclose (FILE *stream);
The function returns an integer number. Returns 0 when a file is closed successfully, otherwise a value other than 0 is returned. You can determine whether a file is closed successfully based on the return value of the function.
Example 10:
#iclude <stdio.h>
Main ()
{
FILE *FP; /* Define a file pointer/*
int i;
Fp=fopen ("Clib", "RB"); /* Open the file with the current directory named Clib Read Only */
if (fp==null)/* To determine whether the file was opened successfully * *
Puts ("File open Error");/* Prompt Open unsuccessful/
I=fclose (FP); /* Close Open File * *
if (i==0)/* To determine whether the file is closed successfully * *
printf ("O,k"); /* Prompt Shutdown Success */
Else
Puts ("File close Error");/* Prompt shutdown unsuccessful */
}
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.