C Programming language

Character I/O : How to use putc( ) Function and getc( ) Function in C
Previous Home Next

getc( ) function and putc( ) function : The getc() and putc() are exactly similar to that of fgetc() and fputc(). The putc function writes the character contained in character variable c to the file associated with the pointer fp1.

int putc (int ch, FILE *file) // define putc() method

int getc (FILE *file)        // define getc() method

The getc() function takes the file pointer as its argument, reads in the next character from the file and returns that character as a integer or EOF when it reaches the end of the file. The integer returned can easily type cast into a character variable for your use.The getc function acts essentially identically to fgetc, but is a macro that expands in-line.

/** Program to Read/Write using getc( ) and putc( ) */
#include<stdio.h>
void main()
{
file *fp1;
char c;
fp1=fopen("myfile.txt","w")
while((c=getchar())!=EOF)
{
putc(c,fp1);
}
fclose(fp1);
fp1=fopen("myfile.txt","r")
while((c=getc(fp1))!=EOF)
{
printf("%c",c)
}
fclose(fp1);
};
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: