codescracker


c

C clearerr()



« CodesCracker Home Next Function »


clearerr()

The clearerr() function is used to resets (i.e., set to zero) the error flag associated with the stream pointed by the stream. The end-of-file indicator is also reset.

clearerr() Syntax

Following is the syntax of the clearerr() function:

#include<stdio.h>
void clearerr(FILE *stream);

The error flag for each stream is initially set to zero by a successful call to fopen(). File errors can occur for a wide variety of reasons, many of them are system dependent. The exact nature of the error is determined by calling the perror() function. This function displays a message which describes the error.

clearerr() Example

Let's consider the following example. This c program copy one file to the another :

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	FILE *ifile, *ofile;
	char ch, fnames[20], fnamed[20];
	printf("Enter the source file name with extension (like demos.txt) : ");
	gets(fnames);
	printf("Enter the destination file name with extension (like demod.txt) : ");
	gets(fnamed);
	ifile=fopen(fnames, "rb");
	if(ifile==NULL)
	{
		printf("Error in opening the source file..!!\n");
		printf("Press any key to exit...\n");
		getch();
		exit(1);
	}
	ofile=fopen(fnamed, "wb");
	if(ofile==NULL)
	{
		printf("Error in opening the destination file..!!\n");
		fclose(ifile);
		printf("Press any key to exit..!!\n");
		getch();
		exit(2);
	}
	while(!feof(ifile))
	{
		ch=getc(ifile);
		if(ferror(ifile))
		{
			printf("Error occurred while reading..!!\n");
			clearerr(ifile);
			break;
		}
		else
		{
			if(!feof(ifile))
				putc(ch, ofile);
				if(ferror(ofile))
				{
					printf("Error occurred while writing..!!\n");
					clearerr(ofile);
					break;
				}
		
		}
	}
	fclose(ifile);
	fclose(ofile);
	getch();
}

« CodesCracker Home Next Function »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test