Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C language file operation questions, please analyze
C language file operation questions, please analyze

/*Header file, included function library, stdio.h is the input and output library of C language, which contains the most common functions. If you need a certain function, you can man it. There is an introduction to the function function above. And the function usage method, that is, the header file that needs to be quoted. */

#include

/*Function entry, the C language program execution program starts from the main function*/

main ()

{

/*Define a file identification number pointer. When a file is opened later using the fopen function, the pointer will point to the content of the file*/

< p>FILE *fp;

/*Define an integer i and k, an integer array a with a capacity of 6, and its first address is a*/

int i,a [6]={1,2,3,4,5,6},k;

/*fopen function: Function function: Open a file

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

Related functions: open, fclose, fopen_s[1]?, _wfopen

Required library:

Return value: After the file is successfully opened, the file pointer pointing to the stream will be returned. If the file opening fails, NULL is returned and the error code is stored in errno.

Generally speaking, after opening a file, some file reading or writing actions will be performed. If the file opening fails, the subsequent reading and writing actions will not proceed smoothly, so please do after fopen() Error judgment and processing

*/

fp = fopen("data.dat","w+");

/*Save the first address of array a An integer a[0] is input into the data.dat file pointed to by the file pointer fp*/

fprintf(fp,"%d\n",a[0]);

for (i=1;i<6;i++)

{

/*

Function name: fseek

Function: Relocate the file pointer on the stream

Usage: int fseek(FILE *stream, long offset, int fromwhere);

Description: The function sets the position of the file pointer stream. If the execution is successful, the stream will point to a position offset by offset bytes based on fromwhere. If the execution fails (for example, the offset exceeds the size of the file itself), the position pointed by stream will not be changed.

*/

fseek(fp,0L,0);

/*Here is reading a file from the file data.dat through the file pointer fp Write the integer into k*/

fscanf(fp,"%d",&k);

/*Offset the file pointer to the first address of the file*/

fseek(fp,0L,0);

/*Through the for loop, add k from the second to the sixth number in the array a and then write it to the file*/

fprintf(fp,"%d\n",a[i]+k);

}

rewind(fp);

fscanf(fp,"%d",&k);

fclose(fp);

printf("%d\n",k);

}