fgetc() and fputc() functions in C - C-Tutorial

Latest

Thursday, 10 November 2016

fgetc() and fputc() functions in C


fgetc() and fputc() functions:

            fgetc() and fputc() are character oriented functions that provides same functionality as getc() and putc() functions.

fgetc() function:          fgetc() function is used to read a character from a file that has been opened in read mode.  The general format of the fgetc() function is:

Syntax:            ch = fgetc(FilePointer);

Where,
            ch is character type variable.

fputc() function:          fputc() function is used to write a character into a file that has been opened in write/append mode.  The general format of the fputc() function is:

Syntax:            fputc(ch, FilePointer);

Where,
            ch is character type variable.
Here,
            FilePointer is opened in write/append mode and the function writes the data of ch into the file.

/* COPY THE CONTENTS OF ONE FILE INTO ANOTHER FILE CHARACTER BY CHARACTER*/

#include<stdio.h>
main()
{
            char ch;
            FILE *fp1,*fp2;
            clrscr();
            fp1=fopen("demo.txt","r");
            if(fp1==NULL)
            {
                        printf("\nFILE OPENING ERROR");
                        exit();
            }
            fp2=fopen("exam.txt","w");
            while((ch=fgetc(fp1))!=EOF)
            {
                        fputc(ch,fp2);
            }
            fclose(fp2);
            fclose(fp1);

}

No comments:

Post a Comment