codescracker


c

C getc()



« Previous Function Next Function »


getc()

The getc() function returns the next character from specified input stream and increments the file position indicator. The character is read as an unsigned char that is converted into an integer

getc() Syntax

#include<stdio.h>
int getc(FILE *stream);

The getc() function returns the EOF if the end of file is reached. Since EOF is a valid integer value, when working with binary files, you must use feof() function to check for end-of-file condition. If getc() encounters an error, EOF is also returned. If working with the binary files, you must use ferror() to check for file errors.

getc() Example

Following c program reads and display the contents of the text file:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	FILE *fp;
	char fname[20], ch;
	printf("Enter the file name with extension (like demo.txt) : ");
	gets(fname);
	fp=fopen(fname, "r");
	if(fp==NULL)
	{
		printf("Error in opening the file..!!\n");
		printf("Press any key to exit..\n");
		getch();
		exit(1);
	}
	while((ch=getc(fp))!=EOF)
	{
		printf("%c",ch);
	}
	fclose(fp);
	getch();
}

« Previous Function Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test