File & Exceptional HAndling
File & Exceptional HAndling
File & Exceptional HAndling
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *f;
f = fopen("abc.txt","w");
if(f == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(f,"%d",num);
fclose(f);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *f;
if ((f = fopen("abc.txt","r")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
fscanf(f,"%d", &num);
printf("Value of n=%d", num);
fclose(f);
return 0;
}
// Exception handling
#include <stdio.h>
#include <errno.h>
int main()
{
FILE * f;
f = fopen("cdf.txt", "r");
#include <stdio.h>
#include <errno.h>
int main()
{
int errno;
while(1)
{
printf(" Value of errno: ");
scanf("%d",&errno);
return 0;
}