read mode in file in C by R4R Team

To read from file what we require ?-A file pointer.
-fopen() function to open file in read mode.
-fgetc() to read character by character from file.
-fclose() to close the file.

Open file in read mode:Syntax-
fp=fopen("Filename","r");

fgetc()Syntax-
data=fgetc(fp);


program-

#include< stdio.h>
void main()
{
FILE *fp;
char ch;
//open file in read mode
fp=fopen("myfile.txt","r");
//Now read file character by character
printf("Your file data is\nn");
while(1)
{
c=fgetc(fp);
if(c==EOF)
break;
printf("%c",c);
}
fclose(fp);// close file
printf("Your file is safely closed now");
}


output-

Your file data is
This is my data i store inside my file
Your file is safely closed now.




Leave a Comment: