Tag: C read file

How to Read File In C Programming How to read the file in C programming. We will see how to read from a file using different file access modes in C. There are two types of files. One is a binary file and the other is a text file. A binary file stores information in a certain format. The text files store the information in a sequence of characters. There are several ways to read a file. Here are all the blog posts related to C read files.

Reading files is a very important task in any computer system. To read files we can use the functions that are provided in the C language. These functions help us to read the data of the file. For example, we can use the functions that are given below.


#include
#include
int main()
{
FILE *fp;
char name[]="a.txt";
fp=fopen(name,"r");
if(fp==NULL)
printf("Cannot open the file\n");
else
{
fscanf(fp,"%s",name);
printf("The contents of the file are %s\n",name);
}
return 0;
}


#include
#include
int main()
{ FILE *fp; char name[]="a.txt";
fp=fopen(name,"r");
if(fp==NULL) printf("Cannot open the file\n");
else { fscanf(fp,"%s",name);
printf("The contents of the file are %s\n",name);
}
return 0;
}