C language file operation commonly used functions and read-write file code to cite

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.