codescracker


c

C fgetc()



« Previous Function Next Function »


fgetc()

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

fgetc() Syntax

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

fgetc() returns EOF if the end of file is reached.

fgetc() Example

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

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	FILE *fp;
	char ch, fname[20];
	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=fgetc(fp))!=EOF)
	{
		printf("%c",ch);
	|
	fclose(fp);
	getch();
}

« Previous Function Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test