Function Introduction
Function:Open a file
Function prototype:File * fopen (const char * path, const char * mode );
Related functions:Open,Fclose, Fopen_s[1], _ Wfopen
Required libraries:<Stdio. h>
Return Value:After the file is successfully opened, the file pointer pointing to the stream will be returned. If the file fails to be opened, null is returned and an error is returned.CodeIn errno.
In general, after opening a file, some operations will be performed to read or write the file. If the file fails to be opened, the subsequent read/write operations will not proceed smoothly () then, make error judgment and handling.
Parameter description:
The parameter path string contains the path and file name of the file to be opened. The parameter mode string represents the stream format.
Mode has the following forms of strings:
RRead-OnlyOpenFileThe file must exist.
R + can open a file in read/write mode. The file must exist.
RB + read/write opens a binary file and allows reading data.
RW + read/write opens a text file, allowing reading and writing.
W. Open and write only the file. If the file exists, the file length is 0, indicating that the file content will disappear. If the file does not exist, the file is created.
W + open the readable and writable file. If the file exists, the file length is cleared to zero, that is, the file content disappears. If the file does not exist, the file is created.
A. Open and write-only files as an attachment. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (EOF reserved)
A + opens readable and writable files by appending them. If the file does not exist, the file will be created. If the file exists, the written data will be added to the end of the file, that is, the original content of the file will be retained. (The original EOF is not retained)
WB only opens or creates a new binary file; only data can be written.
WB + enables read/write operations or creates a binary file, allowing read and write operations.
Wt + read/write opens or creates a text file; read/write is allowed.
At + read/write opens a text file, allowing you to read or append data at the end of the text.
Open a binary file through AB + read/write, and allow you to read or append data to the end of the file.
The preceding morphological string can be added with a B character, such as a combination of Rb, W + B, and AB +. The B character is added to indicate that the file opened by the function library is a binary file, rather than text files. However, this character is ignored in the POSIX System in Linux. The new file created by fopen () has the s_irusr | s_iwusr | s_irgrp | s_iwgrp | s_iroth | s_iwoth (0666) Permission. For the File Permission, see umask value.
Some C compilation systems may not provide all of these functions. Some C versions do not use "R +", "W +", "A +", but use "RW ", "WR", "Ar", etc. Readers should pay attention to the rules of the system used.
ProgramExample
Example 1
# Include <stdio. h>
# Include <stdlib. h> // to use exit ()
Int main ()
{
Char ch;
File * FP;
Char fname [50]; // used to store file names
Printf ("input file name :");
Scanf ("% s", fname );
Fp = fopen (fname, "R"); // read only
If (FP = NULL) // if it fails
{
Printf ("error! ");
Exit (1); // stop the program
}
// GETC () is used to obtain a character from the open file.
While (CH = GETC (FP ))! = EOF)
Putchar (CH );
Fclose (FP); // close the file
Return 0;
}
Note! Beginners often make a mistake, that is, they do not add a suffix when entering the file name. Please note that!
Program example 2 [2]
# Include <stdio. h>
File * stream, * stream2;
Int main (void)
{
Int numclosed;
// Open for read (will fail if file "crt_fopen.c" does not exist)
If (Stream = fopen ("crt_fopen.c", "R") = NULL) // c4996
// Note: fopen is deprecated; Consider using fopen_s instead
Printf ("the file 'crt _ fopen. c' was not opened \ n ");
Else
Printf ("the file 'crt _ fopen. c' was opened \ n ");
// Open for write
If (stream2 = fopen ("data2", "W +") = NULL) // c4996
Printf ("the file 'data2 'was not opened \ n ");
Else
Printf ("the file 'data2 'was opened \ n ");
// Close stream if it is not null
If (Stream)
{
If (fclose (Stream ))
{
Printf ("the file 'crt _ fopen. c' was not closed \ n ");
}
}
// All other files are closed:
Numclosed = _ fcloseall ();
Printf ("number of files closed by _ fcloseall: % u \ n", numclosed );
}