Q) Write a C program to open a file
Sol)
#include<stdio.h>
#include<stdlib.h>
int main()
{
ch, file_name[24];
FILE *fp;
printf("c-programcodes.blogspot.in");
printf("Enter the name of file you wish to see\n");
get(file_name,"r"); //read mode
if(fp==NULL)
{
perror("Error while opening the file \n") ;
exit(EXIT_FAILURE) ;
}
printf("The contents of %s file are: \n", file_name);
while((ch=fgetc(fp)) != EOF)
printf("%c",ch) ;
fclose(fp);
return 0;
}
Sol)
#include<stdio.h>
#include<stdlib.h>
int main()
{
ch, file_name[24];
FILE *fp;
printf("c-programcodes.blogspot.in");
printf("Enter the name of file you wish to see\n");
get(file_name,"r"); //read mode
if(fp==NULL)
{
perror("Error while opening the file \n") ;
exit(EXIT_FAILURE) ;
}
printf("The contents of %s file are: \n", file_name);
while((ch=fgetc(fp)) != EOF)
printf("%c",ch) ;
fclose(fp);
return 0;
}