Error Handling in File Operations

//Write a C 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.

Post a Comment

0 Comments