728x90 AdSpace

Trending

Display Content of file in c

make use of fopen function to open a file, which is in stdio.h library.
FILE *fopen(const char *filename, const char *mode);


mode Description
"r" Open a file for reading. The file must exist.
"w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is considered as a new empty file.
"a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist.
"r+" Open a file for update both reading and writing. The file must exist.
"w+" Create an empty file for both reading and writing.
"a+" Open a file for reading and appending.
/*
author: ba-programmer.blogspot.com
program:- displaying content of  file c
*/
#include
int main()
{
char ch, filename[20];
FILE *source;

printf("Enter name of file  : ");
scanf(" %s",filename);//opening file in read mode
source = fopen(filename, "r");

if( source == NULL )
{
printf("FileNotFound");
exit(0);
}

while( ( ch = fgetc(source) ) != EOF )
printf("%c",ch);//displaying content of a file

fclose(source);//closing file

return 0;
}
Stay connected...!
comment box is waiting for your opinion...!
Display Content of file in c Reviewed by B AProgrammer on 07:40 Rating: 5 make use of fopen function to open a file, which is in stdio.h library. FILE *fopen(const char *filename, const char *mode); mode Descr...

No comments: