List common functions for C language file operations and code for reading and writing files

Source: Internet
Author: User

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 ");}




Related Article

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.