Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

ERROR HANDLING IN FILE OPERATIONS

Posted By: Adalricus Fischer     Category: C Programming     Views: 3731

Write a program to illustrate error handling in file operations.

Code for ERROR HANDLING IN FILE OPERATIONS in C Programming

#include  <stdio.h>                                             
                                                                    
    main()                                                          
    {                                                               
       char  *filename;                                            
        FILE  *fp1, *fp2;                                           
        int   i, number;                                            
                                                                    
        fp1 = fopen("TEST", "w");                                   
        for(i = 10; i <= 100; i += 10)                              
           putw(i, fp1);                                            
                                                                    
        fclose(fp1);                                                
                                                                    
        printf("\nInput filename\n");                               
                                                                    
    open_file:                                                      
        scanf("%s", filename);                                      
                                                                    
        if((fp2 = fopen(filename,"r")) == NULL)                     
        {                                                           
           printf("Cannot open the file.\n");                       
           printf("Type filename again.\n\n");                      
           goto open_file;                                          
        }                                                           
        elsefor(i = 1; i <= 20; i++)                                    
        {  number = getw(fp2);                                      
           if(feof(fp2))                                            
           {                                                        
              printf("\nRan out of data.\n");                       
              break;                                                
           }                                                        
           else                                                     
              printf("%d\n", number);                               
        }                                                           
                                                                    
        fclose(fp2);                                                
    }                                                               
                                                                    






Output                                                           
                                                                    
   Input filename                                                   
   TETS                                                             
   Cannot open the file.                                            
   Type filename again.                                             
                                                                    
   TEST                                                             
   10                                                               
   20                                                               
   30                                                               
   40                                                               
   50                                                               
   60                                                               
   70                                                               
   80                                                               
   90                                                               
   100                                                            

   Ran out of data.                                                 
  
Share: 


Didn't find what you were looking for? Find more on ERROR HANDLING IN FILE OPERATIONS Or get search suggestion and latest updates.

Adalricus Fischer
Adalricus Fischer author of ERROR HANDLING IN FILE OPERATIONS is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!