C programming code to open a file and print its contents on screen.
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
{
char ch, file_name[25];
FILE *fp;
printf("Enter name of a file you wish to see\n");
gets(file_name);
gets(file_name);
fp = fopen(file_name, "r"); // read mode
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
{
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);
printf("%c", ch);
fclose(fp);
return 0;
}
return 0;
}
Read file C program output:


No comments:
Post a Comment