The standard input/output library provides similar routines for file input/output as well as for standard input/output.
From site : http://ctechrockz.blogspot.com/
getc() and putc()
The two functions getc() and putc() are the most basic functions of C. They are used to read the character and to display the character.
getc():
The function getc() reads a character from a file referenced by file pointer opened in read mode using fopen(). The syntax of getc() is: int getc(File's pointer);
Example: int getc(fp);
getc() returns the next character on the given input stream and increments the stream's file pointer to the next character. It reaches EOF marker after reaching the end of file.
putc():
The function putc() writes a character to the file which is opened by fopen() in write mode.
The syntax of putc() is: int putc(int c,fp);
putc() outputs the character given by 'c' to the file through the pointer 'fp'. It returns the character 'c' on success, that is, when a character is written to that particular which we have been opened. On error, the pointer will points towards end of the file(EOF).
The character input/output operations like getchar(), and putchar() will also work as similar to getc(fp), and putc(c,fp)
Example Program:
A program code using getc(), and putc() operations.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
int c;
fp=fopen("cse.txt","r"); // opens file 'cse' in read mode.
if(fp==NULL)
{
printf("cannot open the file");
exit(0); //terminates the program.
}
c=getc(fp); // reads a character and fp points to the next character.
while(c!=EOF) // checks whether end of file reaches are not; if not the below code will executes.
{
putchar(c); // prints a character 'c' on the screen.
c=getc(fp); // reads next character.
}
fclose(fp); //closes the file which is referenced by the pointer fp.
getch();
Explanation:
In this program, the file named cse.txt is opened in a read mode. Its contents are read character by character using getc(), and then displayed on the screen. When the pointer reaches to EOF, then the while loop terminates and file will be closed using the function called fclose().
From site : http://ctechrockz.blogspot.com/