File related operations

Note: When performing specific operations on the file, the operation flow of "** open (create)-read / write-close" must be followed.

Common file system functions are as follows:

classification Function name
open a file fopen()
Close file fclose()
File location fseek () (change the pointer position of the file position)
rewind () (make the file position at the beginning of the file)
ftell () (return the current value of the file pointer)
File reading and writing fgetc () (read a character from a file)
fgetc () (output a character to a specified file)
fgets () (read a string)
fputs () (output a string to a specified file)
getw () (from a specified Read a word from the file (int type)
putw () (output a word (int type) to the specified file
fread () (read the data from the specified file)
fwrite () (write the data to the specified file )
Fscanf () (input data in the specified file format)
fprintf () (write data to the specified file in the specified format)

1. Opening and closing the file

1. Use the fopen () function to open the file. Its usage is:
FILE *fopen(char *filename, char *mode);

filename is the file name (including the file path), mode is the open mode, they are all strings.

How to use the file significance
r(read) read
w(write) write
a(append) add to
t(text) Text file, can be omitted without writing
b(binary) Binary text
+ Read and write

E.g:

FILE *fp = fopen("demo.txt", "r");

Indicates to open the demo.txt file in the current directory in "read-only" mode, and make fp point to the file. fp is often called a file pointer.

2. Use fclose to close the file

int fclose(FILE *fp);

E.g:

fclose(fp);

When the file is normally closed, the return value of fclose () is 0. If a non-zero value is returned, it indicates that an error has occurred.

2. File read and write functions

1.fget () and fput ().
fgetc function: Read a character from the specified file.
The function prototype is: `int fgetc (FILE * fp)`


fputc function: write a character to the specified file, if the writing is successful, return to output the character, otherwise return EOF. The
prototype of the function is: int fputc (char ch, FILE * fp)

2.fgets和fputs.

1. The fgets () function is used to read a string from a specified file and save it to a character array. Its function prototype is:

char *fgets ( char *str, int n, FILE *fp );

str is a character array, n is the number of characters to be read, and fp is a file pointer.

Return value: the first address of the character array is returned when the reading is successful, that is, str; NULL is returned when the reading fails; if the internal pointer of the file has already pointed to the end of the file when the reading is started, then no characters can be read, and NULL is also returned

2. The fputs () function is used to write a string to the specified file. Its function prototype is:

int fputs( char *str, FILE *fp );

Successful write returns non-negative number, fails returns EOF


3.fscanf和fprintf.

fscanf: Format the read data from the specified file. The function prototype is:

int fscanf(FILE *fp,"格式化字符串",【输入项地址表】);

fprintf: Format and write data to the specified file, the function prototype is:

int fprintf(FILE *fp,"格式化字符串",【输入项地址表】);

fprintf () returns the number of characters successfully written, and returns a negative number if it fails. fscanf () returns the number of parameters successfully assigned in the parameter list.

4.fread和fwrite.

fread: read a set of data from the specified file

fread(buffer, size, count, file);

buffer The first address of the memory that stores the read data
size The number of bytes per data item
count The number of data items
file File type pointer

fwrite: Write a set of data to a specified file

fwrite( buffer, size, count, file);

buffer The first address of the output data
size The number of bytes per data item
count The number of data items
file pointer to file type

Three. File positioning

1. Rewind function.
rewind function: move the read-write pointer of the file to the beginning of the file
void rewind(FILE *fp);
2. The fseek function.
fseek function: used to move the position pointer to an arbitrary position
int fseek ( FILE *fp, long offset, int whence );

Moves the file pointer from when address to offset address.

3.ftell function.
ftell function: returns the current read and write position of the file, and returns (-1L) if an error occurs.
ftell(文件指针);
Published 10 original articles · Likes2 · Visits 217

Guess you like

Origin blog.csdn.net/dfwef24t5/article/details/103754993