The next day-------Fgetc,fputc,fgets,gputs,fread,fwrite

Source: Internet
Author: User
Tags fread

int getchar (void);

The GetChar function is equivalent to GETC (stdin). Read one character from standard input

int getc (FILE *stream);

Return value: The return value is usigned char but we treat it with the int type

is typically defined as a macro to use

int fgetc (FILE *stream);

Is the same as the GETC function, but is generally defined as function usage

Macros and Functions:

Macro does not occupy the scheduling time, only takes up the compilation time;

The function occupies the scheduling time, does not occupy the compile time;

 is equivalent to PUTC (c, stdout).

Output point to standard output

int FPUTC (int c, FILE *stream);

Output to the specified stream file

Example 1: File copy

/*./mycpy src dest*//*function, using FPUTC fgetc to implement file copy function*//*argv[1]:src argv[2]:d est*/#include<stdio.h>#include<stdlib.h>intMainintARGC,Char**argv) {FILE*fps; FILE*FPD; intch; if(ARGC <3) {fprintf (stderr,"usage:%s <src.file> <dest.file>\n", argv[0]); Exit (1); }    //Open Sourcefps = fopen (argv[1],"R"); if(fps = =NULL) {Perror ("fopen ()"); Exit (1); }    //Open Target StreamFPD = fopen (argv[2],"w+"); if(FPD = =NULL) {Perror ("fopen ()"); Fclose (fps);//when the target stream fails to open, the source will be closedExit1); }     while(1) {ch=fgetc (fps); if(ch = = EOF)//If you read the end of the file or read an error             Break;    FPUTC (CH,FPD);    } fclose (FPD);    Fclose (fps); Exit (0);}

Example two: Calculating the size of a file (fgetc FPUTC use)

/*calculate the size of a file*/#include<stdio.h>#include<stdlib.h>intMainintARGC,Char**argv) {FILE*FP; intCH =0 ; intCount =0 ; if(ARGC <2) {fprintf (stderr,"usage:....\n"); Exit (1); } FP= fopen (argv[1],"R"); if(fp = =NULL) {Perror ("fopen ()"); Exit (1); }     while(1) {ch=fgetc (FP); if(ch = =EOF) Break; Count++; } fprintf (stdout,"count =%d\n", Count);    Fclose (FP); Exit (1);}

The next day-------Fgetc,fputc,fgets,gputs,fread,fwrite

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.