List common functions for C language file operations and code for reading and writing files
Common functions for file operations
Fopen () Open stream fclose () Close stream fputc () Write a character to stream fgetc () read a character fseek () from stream () locate the specified character fputs () in the stream and write the string to the stream fgets (). Read one line from the stream or specify the character fprintf () and output it to the stream fscanf () in the format () when feof () is read from the stream in the format and reaches the end of the file, the true value ferror () is returned. When an error occurs, the value rewind () is returned. Reset the file locator and remove () at the beginning of the file () delete file fread () read a specified number of characters from the stream fwrite () Write a specified number of characters to the stream tmpfile () generate a temporary file stream tmpnam () generate a unique file name
// Write the FILE void main () {int I = 0 by character; // The cyclic variable FILE * fp = NULL; char buf [] = "alkdjlkfjdlkjfodijfoidjdijfkldjfk "; // General char * filename = "D:/1.txt" in Linux and Windows; // file path name // open stream fp = fopen (filename," a + "); if (fp = NULL) {printf ("func fopen () err \ n"); return;} for (I = 0; I
// Read the FILE void main () {FILE * fp = NULL by character; char * filename = "D:/1.txt"; // open the stream fp = fopen (filename, "r"); if (fp = NULL) {// failed to open the stream printf ("func fopen () err \ n"); return;} while (! Feof (fp) {char tep = fgetc (fp); printf ("% c", tep) ;}fclose (fp); system ("pause ");}