How to Read From a File in C?
Last Updated :
17 Jun, 2024
File handing in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program.
In this article, we will explore how to read from a file in C using standard library functions.
Read From a File in C
In C, we can read files using functions provided by the standard library such as fopen(), fgetc(), fgets(), and fread().
Steps To Read A File:
- Open a file using the function fopen() and store the reference of the file in a FILE pointer.
- Read contents of the file using any of these functions fgetc(), fgets(), fscanf(), or fread().
- File close the file using the function fclose().
Lets try to read contents of the file test.txt that contains following content
GeeksforGeeks | A computer science portal for geeks
C Program to Read a File
C
// C program to implement
// the above approach
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Driver code
int main()
{
FILE* ptr;
char ch;
// Opening file in reading mode
ptr = fopen("test.txt", "r");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
// Printing what is written in file
// character by character using loop.
do {
ch = fgetc(ptr);
printf("%c", ch);
// Checking if character is not EOF.
// If it is EOF stop reading.
} while (ch != EOF);
// Closing the file
fclose(ptr);
return 0;
}
Output
GeeksforGeeks | A computer science portal for geeks
In this program, we will read contents of the file using fgetc(). fgetc() reads characters pointed by the function pointer at that time. On each successful read, it returns the character (ASCII value) read from the stream and advances the read position to the next character. This function returns a constant EOF (-1) when there is no content to read or an unsuccessful read.
Similar Reads
How to Read a Struct from a Binary File in C? The structure in C allows the users to create user-defined data types that can be used to group data items of different types into a single unit. We can save this structure in a file using C file handling. In this article, we will learn how we can read a struct from a binary file in C. Read a Struct
2 min read
How to Read a File Line by Line in C? In C, reading a file line by line is a process that involves opening the file, reading its contents line by line until the end of the file is reached, processing each line as needed, and then closing the file.Reading a File Line by Line in CReading a file line by line is a step by step:1. Opening th
3 min read
C Program to Read Content of a File In C, reading a file is a step-by-step process in which we first have to prepare the file only after which we can start reading. It involves opening the file, reading its data, and closing the file.Opening the File: Opening the file loads the file into the memory and connect the file to the program
5 min read
How to Read Input Until EOF in C? In C, reading input until the End of the File (EOF) involves reading input until it reaches the end i.e. end of file. In this article, we will learn various methods through which we can read inputs until EOF in C.Reading Input Until EOFRead Input Until EOF Using getchar()Read Input Until EOF Using f
5 min read
How to write in a file using fputs() in C fputs() is a function declared in stdio.h header file. It is used to write the contents of the file. The function takes 2 arguments. The first argument is a pointer to the string which is to be written and the second argument is the pointer of the file where the string is to be written. It returns 1
2 min read