Common functions for file operations
fopen () Open stream fclose () closes the stream FPUTC () writes a character into the stream fgetc () reads a character from the stream fseek () navigates to the specified character in the stream fputs () writes the string to the stream fgets () Reads a line from the stream or specifies a character fprintf () output to stream fscanf () from the stream in a format read feof () when the end of the file is reached Ferror () returns its value when the error occurs rewind () resets the file locator to the beginning of the file remove () Delete file fread () reads a specified number of characters from the stream fwrite () writes a specified number of characters to the stream tmpfile () generates a temporary file stream Tmpnam () generates a unique file name
Write file by character void Main () {int i=0;//loop variable file *fp=null;char buf[]= "ALKDJLKFJDLKJFODIJFOIDJDIJFKLDJFK";//linux and win under Universal Char *filename= "D:/1.txt";//file path name//open stream Fp=fopen (filename, "A +"); if (fp==null) {printf ("func fopen () err\n"); return;} for (i=0; I<strlen (BUF); i++) {//characters to stream FPUTC (BUF[I],FP);} Close Stream fclose (FP); system ("Pause");}
Reads the file by character Void Main () {file *fp=null;char *filename= "D:/1.txt";//Open stream Fp=fopen (filename, "R"); if (fp = = NULL) {// Open stream failed printf ("Func fopen () err\n"); return;} while (!feof (FP)) {char tep=fgetc (FP);p rintf ("%c", TEP);} Fclose (FP); system ("Pause");}
C language file operation commonly used functions and read-write file code to cite