Prototypes and notes of C standard file io functions

Publisher:Tiger8Latest update time:2018-11-22 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 fopen()


Requires header file: #include


Function prototype: FILE *fopen(const char *path,const char *mode)


Function parameters: path: the path and file name of the file to be opened


mode: file opening method, see below


Function return value: Success: FILE type pointer pointing to the file


Failure: NULL


The following are the allowed values ​​and descriptions of the mode parameter:


r or rb opens the file in read-only mode. The file must exist


r+ or r+b opens the file in a readable and writable manner. The file must exist


w or wb opens the file in write-only mode. If the file does not exist, it is created; if the file exists, the original content of the file is erased and the file is operated from the beginning.


w+ or w+b opens the file in a readable and writable manner. If the file does not exist, it is created; if the file exists, the original content of the file is erased and the file is operated from the beginning.


a or ab opens a write-only file in append mode. If the file does not exist, it is created; if the file exists, the written data is appended to the end of the file, that is, the original content of the file will be retained.


a+ or a+b opens a readable and writable file in append mode. If the file does not exist, it is created; if the file exists, the written data is appended to the end of the file, that is, the original content of the file will be retained.


}


gets(), fgets()


Requires header file: #include


Function prototype: char *gets(char *s)


char *fgets(char *s,int size,FILE *stream)


Function:


Assume that the length of buff is MAX; then there are two ways to stop fgetc:


1. When reading /n, write the ascii of /n into buff and stop, and add a '\0' at the end;


2. When size-1 characters are read but no /n is read, the sequence ends and a '\0' is added at the end;


Function parameters: s: buffer address where input characters are stored


size: the length of the input string


stream: input file stream


Function return value:


Success:s


Failure or end of file: NULL


puts(), fputs()


Requires header file: #include


Function prototype: int puts(const char *s)


int fputs(const char *s,FILE *stream)


Function parameters: s: buffer address where output characters are stored


stream: output file stream


Function return value: Success: non-negative number


Failure: EOF


scanf(), fscanf(), sscanf()


Requires header file: #include


Function prototype: int scanf(const char *format,...);


int fscnaf(FILE *fp,const char *format,...);


int sscanf(char *buf,const char *format,...);


Function parameters: format: input format


fp: stream to be input


buf: buffer to be input


Function return value: Success: the number of data read


Failure: EOF


printf(), fprintf(), sprintf()


Requires header file: #include


Function prototype: int printf(const char *format,...);


int fprintf(FILE *fp,const char *format,...);


int sprintf(char *buf,const char *format,...);


Function parameters: format: output format


fp: stream to be output


buf: buffer to be output


Function return value: Success: Number of characters output


Failure: EOF


fread()


Requires header file: #include


Function prototype: size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);


Function parameters: ptr: buffer for storing read data


size: The size of each data item read (in bytes)


nmemb: number of data read


stream: the stream to read


Function return value:


Success: The actual number of nmemb read


Failure: 0


fwrite()


Requires header file: #include


Function prototype: size_t fwrite(void *ptr,size_t size,size_t nmemb,FILE *stream);


Function parameters: ptr: buffer to store written data


size: The size of each data item written (in bytes)


nmemb: Number of data written


stream: the stream to write to


Function return value: Success: The actual number of nmemb written


Failure: 0


The feof()/ferror() functions can be used to determine whether the read operation is wrong or the file has been read to the end.


When opening a stream, the offset is 0 (the beginning of the file).


fseek()


Requires header file: #include


Function prototype: int fseek(FILE *stream, long offset, int whence);


Function parameters: stream: the stream to be positioned


offset: offset relative to the reference point whence


whence: reference point (values ​​see below)


Function return value: Success: 0, change the read and write position


Failure: EOF, no change in read or write position


The value of the third parameter whence is as follows:


SEEK_SET: represents the starting position of the file, the number is 0


SEEK_CUR: represents the current read and write position of the file, the number is 1


SEE

K_END: ​​represents the end position of the file, the number is 2


Function ftell()


Requires header file: #include


Function prototype: int ftell(FILE *stream);


Function parameters: stream: the stream to be positioned


Function return value: Success: Returns the current read and write position


Failure: EOF


Reference address:Prototypes and notes of C standard file io functions

Previous article:Embedded C code optimization
Next article:How to use C language variable parameter function

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号