Handle Errors When File Opening Fails and displays these contents on the screen. - C File

C examples for File:File Write

Description

Handle Errors When File Opening Fails and displays these contents on the screen.

Demo Code

#include <stdio.h>

int main()/*from   www .j  a v a2  s  .c o  m*/
{
     int num, k = 0;
     FILE *fptr;
     fptr = fopen("C:\\Code\\data.txt", "r");
     if (fptr == NULL) {
        puts("File-opening failed");
        return 0;
     }
     puts("File is opened successfully");
     num = fgetc(fptr);
     while(!feof(fptr)) {
       putchar(num);
       num = fgetc(fptr);
     }
   
     k = fclose(fptr);
     if(k == -1)
       puts("File-closing failed");
     else
       puts("File data.txt is closed successfully");
     
     return(0);
}

Result


Related Tutorials