![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtrnDcp6XhV3yzZudg5a0vEA8mZvPbvCFdvqCQYTZLcwPl7BoHyKVRTIFMJY-r_5eMG_t2rciJLb_C2GAYyDW7zMA4P3Pt34J20W3lIhTnbIFtksbLcbLs_n-hSIav4St5wHUPHJIqIAXK/s640/to+display+the+contents+in+a+File.jpg)
By executing this program screen will display text in a file. you need to enter file name with extension and file should be in directory folder.
Program:
//Program To show content in file. by SlashMyCode.com
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter the name of file you wish to see\n");
gets(file_name);
fp = fopen(file_name,"r");
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);
//Program To show content in file. by SlashMyCode.com
fclose(fp);
return 0;
}
Output:
This is text document in directory folder, with name 'SlashMyCode.txt'.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgd3Hrketn-DcwYIv5aRU1LyjN5ZuV1ROPsBm4UbsjATswsgOfLhL9NHZbNyMveTkIK8PHyI932S6pdC6i5k0SWn1eYJmj-g4RCRiaK_tUbLursM-ull9mu7dwGFj29lY4m_2pmb9k87ATj/s640/Output+of+C+program+to+display+the+contents+in+a+File.jpg)
Just enter the file name with its extension, and program will display text content in it.
No comments:
Post a Comment
Feel free to ask us any question regarding this post