C-language basic file read/write operations, C-language basic read/write operations
I have compiled a C-language file read/write operation code, and opened the corresponding comments during the test.
1 # include <stdio. h> 2 # include <stdlib. h> 3 # include <unistd. h> 4 # include <string. h> 5 6/* 7 * EOF: a symbolic constant whose value is-1. 8 * fgets () can read a maximum of int-1 characters, in case of line feed or EOF is returned. 9 * when writing a file with fputs (), the string cutoff character '\ 0' is ignored '. 10 * fread () and fwrite () data block read/write, mostly used for struct arrays (sequential storage struct ). 11*12 * function prototype: 13 * read: 14 * int fgetc (FILE * stream); 15 * char * fgets (char * s, int size, FILE * stream ); 16 * size_t fread (void * ptr, size_t size, size_t nmemb, FILE * str Eam); 17 * int fscanf (FILE * stream, const char * format ,...); 18 * write: 19 * int fputc (int c, FILE * stream); 20 * int fputs (const char * s, FILE * stream ); 21 * size_t fwrite (const void * ptr, size_t size, size_t nmemb, FILE * stream); 22 * int fprintf (FILE * stream, const char * format ,...); 23 * Others: 24 * void rewind (FILE * stream); point the position pointer inside the FILE to the beginning of a stream (data stream/FILE. 25*26 * Open Mode: 27 * r (read): read 28 * w (wri Te): Write 29 * a (append): append 30 * +: Read and Write 31 * t (text): text File, can be omitted not to write 32 * B (banary ): binary FILE 33 */34 35 FILE * fp = NULL; 36 37 // READ 38 void GetCharFromFile (FILE * fp) 39 {40 int ch = 0; 41 42 while (ch = fgetc (fp ))! = EOF) // EOF 43 {44 printf ("fget: [% c] \ n", ch) is returned when a failure occurs ); 45} 46} 47 48 void GetLineFromFile (FILE * fp) 49 {50 char line [1024] = ""; 51 52 while (fgets (line, sizeof (line ), fp )! = NULL) // if a failure occurs, NULL 53 {54 printf ("fgets: [% s] \ n", line) is returned ); 55} 56} 57 58 void GetBlockFromFile (FILE * fp) 59 {60 int I = 0; 61 int block [10] = {0}; 62 int count = 1; 63 64 while (fread (block, sizeof (block), count, fp) = count) // return value upon failure! = Count 65 {66 printf ("fread:"); 67 for (I = 0; I <10; I ++) 68 {69 printf ("% d ", block [I]); 70} 71 printf ("\ n"); 72} 73} 74 75 void ReadFormatToFile (FILE * fp) 76 {77 char file [32] = ""; 78 char func [32] = ""; 79 char date [32] = ""; 80 int line = 0; 81 82 fscanf (fp, "% s % d % [^ \ n]", file, func, & line, date); // return the number of read elements, eg: 4 83 printf ("file: % s \ n", file); 84 printf ("func: % s \ n", func); 85 print F ("line: % d \ n", line); 86 printf ("date: % s \ n", date ); 87} 88 89 // WRITE 90 int WriteCharToFile (FILE * fp, char ch) 91 {92 int ret = 0; 93 94 ret = fputc (ch, fp ); // return EOF 95 return ret if the request fails! = EOF? 0:-1; 96} 97 int WriteStrToFile (FILE * fp, char * str) 99 {100 int ret = 0; 101 102 ret = fputs (str, fp ); 103 return ret! = EOF? 0:-1; // returns EOF104} 105 106 int WriteBlockToFile (FILE * fp, const void * block, int size, int count) 107 {108 int ret = 0; 109 110 ret = fwrite (block, size, count, fp); // return value upon failure! = Count111 return ret! = Count? -1:0; 112} 113 114 int WriteFormatToFile (FILE * fp) 115 {116 int ret = 0; 117 118 ret = fprintf (fp, "% s % d % s \ n", _ FILE __, _ func __, _ LINE __, _ DATE __); 119 return ret <0? -1:0; // if a failure occurs, a negative value 120} 121 122 // MAIN 123 int main (int argc, char ** argv) 124 {125 char ch = 'R' is returned '; 126 char * str = "Hello World. \ n "; 127 int block [10] = {1, 2, 4, 5, 6, 7, 8, 9, 0}; 128 char * filePath = ". /ll "; 129 130 fp = fopen (filePath," w + "); // ignore whether the file exists or not, rewrite the file each time, and read 131 if (NULL = fp) 132 {133 perror ("fopen"); 134 return-1; 135} 136 137 // WRITE138 // printf ("WriteCharToFile: % s \ n", WriteCharToFile (fp, ch )? "Fail": "Success"); 139 printf ("WriteStrToFile: % s \ n", WriteStrToFile (fp, str )? "Fail": "Success"); 140 // printf ("WriteBlockToFile: % s \ n", WriteBlockToFile (fp, block, sizeof (block), 1 )? "Fail": "Success"); 141 // printf ("WriteFormatToFile: % s \ n", WriteFormatToFile (fp )? "Fail": "Success"); 142 143 rewind (fp); 144 // READ145 // GetCharFromFile (fp); 146 GetLineFromFile (fp ); 147 // GetBlockFromFile (fp); 148 // ReadFormatToFile (fp); 149 150 fclose (fp); 151 return 0; 152}