Operation of standard C library on files
1, Overview: open in Linux file programming is the system call function of UNIX system (including Linux, Mac, etc.), and returns the File Descriptor. It is the index of the file in the File Descriptor table, which is different from the C language library function fopen. Fopen is a C language library function in ANSI C standard. Different kernel APIs should be called in different systems. Returns a pointer to the file structure. Open is a UNIX system call with limited portability, while fopen is a C standard function, so it has good portability. Because open returns a File Descriptor, which is an important concept in UNIX system. All devices in UNIX operate in the form of files. Fopen is used to manipulate ordinary formal files. The open function is the low-level IO function closest to the kernel, which is fast, while fopen is the high-level IO function, which is buffered and slow. Low level file IO runs in kernel mode and high-level file IO runs in user mode. The former must be used with read,write and other functions, and the latter with fread,fwrite and other functions.
2, Related functions: fopen,fwrite,fread,fclose,fputc,feof,fgetc.
3, Function fopen:
#include <stdio.h> FILE *fopen(const char *path, const char *mode); //Return value: successfully returns the file pointer to the stream; Failure returns NULL
① Function: use the given mode to open the file pointed to by filename.
② Parameters:
*path: the name of the file to open.
*Mode: file access mode.
The mode ending with x is exclusive mode. fopen will fail if the file already exists or cannot be created (usually the path is incorrect). The file is opened in an exclusive mode supported by the operating system.
The above morphological strings can be added with a b character, such as rb, w+b or ab +, and the b character is added to tell the function library to open the file in binary mode. If b is not added, it means that t is added by default, that is, rt and wt, where t means to open the file in text mode. The new file created by fopen() will have S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH(0666) permission. This file permission will also refer to the umask value.
Some C compilation systems may not fully provide all these functions. Some C versions do not use "r +", "w +", "a +", but use "rw", "wr", "ar", etc. pay attention to the regulations of the system used.
4, Function fwrite
#include <stdio.h> size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream); //Return value: if successful, a size is returned_ T object, representing the total number of elements, which is an integer data type; If it fails, the return value is less than the value of nmemb
① Function: write the data in the array pointed to by ptr to the given stream.
② Parameters:
*ptr: pointer to the array of elements to be written.
*Size: the size of each element to be written, in bytes.
*nmemb: the number of elements. The size of each element is size bytes.
*Stream: pointer to a FILE object that specifies an output stream.
5, Function fread
#include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); //Return value: if successful, returns the number of read objects; If it fails, the return value may be less than nmemb
Parameters:
*ptr: pointer to the first object in the array to be read
*Size: the size of each object (in bytes)
*nmemb: number of objects to read
*Stream: input stream
6, Function fclose
#include <stdio.h> int fclose(FILE *fp); //Return value: if the file is successfully closed, the return value is 0; Otherwise, return to EOF
① Function: close the file specified by fp
② Function: pointer to the FILE object that specifies the stream to be closed
Example 1:
#include <stdio.h> #include <string.h> int main() { FILE *fp; char *str = "hallo world!"; char readbuf[128] = {0}; fp = fopen("./file2.txt","w+"); fwrite(str,sizeof(char),strlen(str),fp); //fwrite(str,sizeof(char)*strlen(str),1,fp); fseek(fp,0,SEEK_SET); fread(readbuf,sizeof(char),strlen(str),fp); printf("data:%s\n",readbuf); fclose(fp); return 0; }
The compilation results are as follows:
Example 2:
Write a structure to the file
#include <stdio.h> struct Test { int a; char c; }; int main() { FILE *fp; struct Test data = {100,'c'}; struct Test data2; fp = fopen("./file3","w+"); fwrite(&data,sizeof(struct Test),1,fp); fseek(fp,0,SEEK_SET); fread(&data2,sizeof(struct Test),1,fp); printf("data:%d,%c\n",data2.a,data2.c); fclose(fp); return 0; }
Compilation run result:
7, Function fputc
#include <stdio.h> int fputc(int c, FILE *stream); //Return value: this function returns a non negative value. If an error occurs, it returns EOF(-1).
① Function: write the string to the specified stream, but do not include null characters.
② Parameters:
*str: This is an array containing the null terminated character sequence to be written.
*Stream: pointer to the FILE object that identifies the stream to be written to the string
example:
#include <stdio.h> #include <string.h> int main() { FILE *fp; int i; char *str = "hallo world"; int len = strlen(str); fp = fopen("./test.text","w+"); for(i=0;i<len;i++) { fputc(*str,fp); str++; } fclose(fp); return 0; }
The compilation results are as follows
test. Contents in text:
8, Function feof
eof is a C language standard library function, and its prototype is in stdio H, its function is to detect the end of the file on the stream. If the file ends, it returns a non-0 value, otherwise it returns 0 (that is, end of file: return a non-0 value; file not ended: return a 0 value).
#include <stdio.h> int feof(FILE *stream); //Return value: if the file ends, the return value is non-0; The file is not finished, and the return value is 0
① Function: detect the end of file on the stream.
② Parameters: same as above.
Note: feof determines whether the file ends by reading the error returned by the function fread/fscanf, so it should be determined after reading the function. For example, when reading a file in a while loop, if you make a judgment before reading the function, if the last line of the file is a blank line, a memory error may be caused.
9, Function fgetc
#include <stdio.h> int getc(FILE *stream); //After the function is executed successfully, the read characters will be returned
① Function: read a character from the file pointed to by the file pointer stream. After reading a byte, move the cursor position back by one byte.
② Parameters: same as above.
#include <stdio.h> int main() { FILE *fp; int i; char c; fp = fopen("./test.text","r"); while(!feof(fp)) { c = fgetc(fp); printf("%c",c); } printf("\n"); fclose(fp); return 0; }
In the will statement, when the file is not finished and the feof return value is 0, continue to execute the while loop; When the file ends, the return value of feof is non-0, and the while loop will jump out