C file operations currently use functions in the C library, including fwrite fread fgets fputs GETC putc fopen fclose fseek fgetline
When talking about these functions, Liu gave us enough time to study these functions. Run the man command to view the data manuals of these functions. The first is the input parameter, the second is the function, and the third is the return value of the function. More functions will be exposed in the future. It is impossible for the teacher to talk about it. Therefore, it is very important to read the manual.
When using external functions, You must judge the results. Use the perror function to analyze errors.
The function for reading content from a file is fwrite fgets GETC Getline.
Fread can specify how much data is read each time. fgets reads a string of characters each time, GETC reads a character each time, and fgetline reads a row each time.
The function for writing data to a file is fwrite, fputs, and putc, which is opposite to the above function.
Both the fopen and fclose functions are used for file operations.
Fseek file pointer offset function. The third parameter is the offset position. Seek_set seek_end seek_out seek_cur
1 #include<stdio.h> 2 3 int main() 4 { 5 FILE * fp; 6 if((fp = fopen("hello","r"))==NULL){ 7 perror("open"); 8 return 1; 9 }10 11 int i = 0;12 char c ;13 for(i=0;i<5;i++){14 c = getc(fp);15 printf("%c",c);16 }17 printf("\n");18 fseek(fp,0,SEEK_SET);19 for(i=0;i<5;i++){20 c = getc(fp);21 printf("%c",c);22 }23 printf("\n");24 25 }
The fgetline function is the most complex function. It also involves second-level pointers and heap operations. Homework in the evening is just a question. Each row in the source file contains three parts: Student ID, name, and score. You need to save the information to the struct and sort it and output it to the file. Separate each part of the source file with the tab key
1 # include <stdio. h> 2 # include <string. h> 3 # include <stdlib. h> 4 struct person {5 char * number; 6 char * Name; 7 int score; 8 9}; 10 int main () 11 {12/* Open the input file, create an output file. 13*14 */15 file * In = NULL; 16 file * out = NULL; 17 in = fopen ("table", "R "); 18 if (in = NULL) {19 perror ("open"); 20 return 1; 21} 22 out = fopen ("sorttable", "W "); 23 if (out = NULL) {24 perror ("open"); 25 return 1; 26} 27/* 28 * truncate the content in line and store it in the pointer array. 29*30 */31 char * line = NULL; 32 size_t N; 33 ssize_t Len; 34 char * P [20] = {0}; 35 char * k [20] = {0}; 36 int I = 0; 37 Int J = 0; 38 While (LEN = Ge Tline (& line, & N, in ))! = EOF) {39 * (p + I) = malloc (LEN); // remember Free 40 for (j = 0; j ++) {41 if (* (LINE + J) = '\ n') {42 * (p + I) + J) =' \ 0'; 43 break; 44} 45 * (p + I) + J) = * (LINE + J); 46} 47 I ++; 48 49 * (K + I) = * (p + I); 50 51} 52/* 53 * truncate the content of each row into three parts based on \ t and store them in a two-dimensional array. 54*55*56 */57 int M = I; 58 char * SRC; 59 char * TMP [3]; 60 struct person man [m]; 61 for (I = 0; I <m; I ++) {62 for (j = 0; j <3; j ++) {63 src = strstr (* (p + I), "\ t"); 64 * (TMP + J) = strtok (* (p + I ), "\ t"); 65 * (p + I) = SRC + 1; 66} 67 MAN [I]. number = * (TMP + 0); 68 MAN [I]. name = * (TMP + 1); 69 MAN [I]. score = atoi (* (TMP + 2); 70} 71/* 72 * sort the struct array by bubble method 73*74 */75 struct person sttmp; 76 for (I = 0; I <m-1; I ++) {77 for (j = I + 1; j <m; j ++) {78 If (MAN [I]. score <MAN [J]. score) 79 {80 sttmp = MAN [I]; 81 MAN [I] = MAN [J]; 82 MAN [J] = sttmp; 83} 84} 85} 86/* 87 * output the result to the file. 88*89 */90 char show [20]; 91 for (I = 0; I <m; I ++) {92 fputs (MAN [I]. number, out); fputs ("\ t", out); 93 fputs (MAN [I]. name, out); fputs ("\ t", out); 94 sprintf (Show, "% d", man [I]. score); 95 fputs (show, out); fputs ("\ n", out); 96 97} 98 99/* 100 * release heap content and close the file. 101*102 */103 for (I = 0; I <m; I ++) 104 free (* (K + I); 105 if (line) 106 free (line); 107 108 fclose (in); 109 fclose (out); 110 111}
12th days: C-based file operations