Function C (fopen function)

Source: Internet
Author: User
Function prototype: file * fopen (const char * path, const char * mode); related functions: open, fclose, fopen_s, _ wfopen 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 the error code is stored in errno. Function Introduction Function prototype:File * fopen (const char * path, const char * mode ); 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 the error code is stored in errno. In general, after opening a file, some file reads or writes will be performed. If opening the file fails, 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 character strings: R opens a file in read-only mode and the file must exist. R + can open a file in read/write mode. The file must exist. RB + open a binary file to read and write data. The file must exist. 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. (The EOF is retained) 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 writes to open or create a new binary file; only data can be written. WB + enables read/write operations or creates a binary file, allowing read and write operations. Open a binary file through AB + read/write, and allow you to read or append data to the end of the file. At + open a file named string, and a indicates append, which means that the existing content of the original file is written at the time of writing, instead of overwriting from the beginning, t indicates that the type of the opened file is a text file, and the + number indicates that the file can be read or written. 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 instruct the function library to open the file in binary mode. If B is not added, t is added by default, namely RT and wt. t indicates opening the file in text mode. 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. Binary Differences from text mode1. in windows, in text mode, "\ r \ n" indicates a line break. If you open a file in text mode and use functions such as fputs to write the line break "\ n", the function automatically adds "\ r" to "\ n ". That is, "\ r \ n" is actually written to the file ". 2. in Unix/Linux-like text mode, "\ n" indicates a line break. Therefore, in Linux, there is no difference between the text mode and the binary mode. Summary of opening Methods: There are three main differences between opening methods:① Whether to open a binary file, marked with "B. ② There are several read/write Methods: Read-only, write-only, read/write, append-only, and append read/write. ③ There are different responses to whether the file must exist, whether it is cleared or appended when it exists. Specific judgment is as follows. Program example Example 1  #include<stdio.h>  #defineF_PATH"d:\\myfile\\file.dat" intmain( void ) { FILE *fp=NULL; // Note fp= fopen (F_PATH, "r" ); if (NULL==fp) { return -1; // Return the error code } fclose (fp); fp=NULL; // It must be pointed to NULL; otherwise, it will point to the original open file address return0; }  Example 2  #include<stdio.h> # Include <stdlib. h> // to use exit () intmain() { charch; FILE *fp=NULL; charfname[50]; // Used to store the file name printf ( "Input file name :" ); scanf ( "%s" ,fname); fp= fopen (fname, "r" ); // Read only if (fp==NULL) // If it fails { printf ( "Error! " ); exit (1); // Abort the program } while ((ch= getc (fp))!=EOF) putchar (ch); fclose (fp); // Close the file return0; }  Example 3  #include<stdio.h>  FILE *stream,*stream2;  intmain( void ) { intnumclosed; //Openforread(willfailiffile"crt_fopen.c"doesnotexist) if ((stream= fopen ( "crt_fopen.c" , "r" ))==NULL) //C4996 //Note:fopenisdeprecated;considerusingfopen_sinstead printf ( "Thefile‘crt_fopen.c‘wasnotopened\n" ); else printf ( "Thefile‘crt_fopen.c‘wasopened\n" ); //Openforwrite if ((stream2= fopen ( "data2" , "w+" ))==NULL) //C4996 printf ( "Thefile‘data2‘wasnotopened\n" ); else printf ( "Thefile‘data2‘wasopened\n" ); //ClosestreamifitisnotNULL if (stream) { if ( fclose (stream)) { printf ( "Thefile‘crt_fopen.c‘wasnotclosed\n" ); } } //Allotherfilesareclosed: numclosed=_fcloseall(); printf ( "Numberoffilesclosedby_fcloseall:%u\n" ,numclosed); } 

Function C (fopen function)

Related Article

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.