Description
Today we learn about a topic of file handling that how to read the file.
Lets get Started...
#include<stdio.h>
#include<stdlib.h>
int main(){
char ch;
FILE *f_pointer;
f_pointer = fopen("./example.txt", "r");
if(f_pointer == NULL){
perror("Error Occurs!!");
exit(EXIT_FAILURE);
}
printf("Content of the File : \n");
ch = fgetc(f_pointer);
while (ch != EOF)
{
printf("%c", ch); // printing one by one characters from the file
ch = fgetc(f_pointer);
}
fclose(f_pointer);
return 0;
}
- 'ch' variable store the latest seek pointer where read the file.
- f_pointer is a file pointer.
- using the file f_open() function open the file. f_open() function take two arguments first argument is filename of file and second argument mode in which mode open the file.
- check condition if (f_pointer == NULL) -> perror("Error Occur") and perror() function also print the error that means whats error occur. and the exit to the program otherwise.
- print content of the file.
- ch == fgetc(f_pointer), ch hold the seek pointer where read the file.
- And while ch != EOF
- EOF -> End of the File
- when ch is not reach to the end of the file then read the content and printing one by one character.
- At last close the file using fclose() function.
Please Comment Below...
Recommended Posts
Insertion and deletion in doubly linked list in C program
Insertion and Deletion in Doubly linked list in C programming langauge Data Structure. How to Implement insertion and deletion all operation.
C program to check eligible for vote
Write a program to check that person is eligible for voting or not.
Voting program in C language using switch case
Write a program to check that person is eligible for voting or not using switch case.
C program to convert decimal to binary without Array
We can convert any decimal number into binary number by c program without using array.
A C program for checking whether a given line is a comment
Write a program in c to check whether a given line is comment or not.
Linear search in c | Algorithm of Linear search | c programming
It is the easiest search algorithm because it starts at the beginning and works through each element in a list until the desired element is located.