Knowledge Walls
Venkatesan
Hyderabad, Andhra Pradesh, India
Passcode:
Introduction [cont'd]
Input Output Functions [cont'd]
Structures and Unions [cont'd]
# Preprocessor [cont'd]
Error handling in Files of Programming in C
3436 Views
Error handling 
feof() and ferror() functions are used to handle the errors with file operations in c.

feof() - eof function is used to test file pointer reached end of the file or not. It return 1 when it is reached end otherwise 0.
ferror() - It tells file pointer is opened without any errors. If it returns zero then file opened without any errors.
feof() syntax 
int feof(fp as FILE);
ferror() syntax 
int ferror(fp as FILE);
Program
	#include
	void main(){
	   FILE *fp;
	   int i;
	   int product_no;
	   char product_name[30];
	   clrscr();
	   fp = fopen("RECORD.txt","r");
	   if (ferror(fp) != 0){
	       printf("File has error");
	       exit(0);
	   }
	   while (1){
	       if (feof(fp))
	       break;
	       fscanf(fp,"%d %s",&product_no,product_name);
	       printf("%d : %s\n",product_no,product_name);
	   }
	   fclose(fp);
	}
Output 
101 : Hamam
102 : Liril
103 : Himalaya
104 : Colgate
105 : Lux
Next Topics
Next lessons of current book.
# Preprocessor of Programming in C
# Preprocessor of Programming in C
# Preprocessor of Programming in C
# Preprocessor of Programming in C
Previous Topics
Previous lessons of current book.
Best Lessons of "Programming in C"
Top lessons which are viewed more times.
Structures and Unions of Programming in C
Input Output Functions of Programming in C
Structures and Unions of Programming in C
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details