Open In App

C Program to Print Contents of File

Last Updated : 07 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

C language allows users to process the files in its programs. 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. In this article, we will learn how to read and print the contents of a file using C program.

The simplest method to print the whole contents of a file is by using the fgetc() function to read and print each character one by one till the end of the file is reached. Let’s take a look at an example:

C
#include <stdio.h>

int main() {
  
    // Open file in read mode
    FILE *fptr = fopen("file.txt", "r");  
  
    // Check if the file was opened successfully
    if (fptr == NULL) {  
        return 1;  
    }

    // Read and print each character from the file
    char ch;
    while ((ch = fgetc(fptr)) != EOF) {
        putchar(ch);
    }

    // Close the file after reading
    fclose(fptr);  
    return 0;
}

Assuming the contents of file.txt are:

Hello, Geeks!
This is a test file.

The output will be:

Hello, Geeks!
This is a test file.

Explanation: The program opens the file file.txt in read mode (“r”) using fopen(). If the file cannot be opened, fopen() returns NULL and the execution is stopped. Else, it reads the file by one character at a time using fgetc() in a loop, printing each character until fgetc() returns EOF indicating that the whole file is read. Finally, the file is closed using fclose().

Apart from the getc() function, C also provides other methods to print the contents of a file, though, the other steps remain same. Some of them are as follows:

Using fgets()

The fgets() function is a good choice to print the file line by line. This function reads the given number of characters from the file and stores it in a buffer. This buffer can be then printed after each read and then be used to read the next line.

C
#include <stdio.h>

int main() {
  
    FILE *fptr = fopen("file.txt", "r");  
    if (fptr == NULL) {  
        return 1;  
    }

    // Read and print each line from the file
    char buff[100];
    while (fgets(buff, sizeof(buff), fptr) != NULL) {
        printf("%s", buff);
    }

    fclose(fptr);
    return 0;
}


Output

Hello, Geeks!
This is a test file.

Explanation: In this program, fgets() is used to read one line at a time from the file file.txt. It stores the read content into the buff array, up to 99 characters, and includes the newline character if present. The function ensures safe reading by limiting the number of characters and appends a null terminator ‘\0’ at the end of each line.

Using fread()

The fread()function can read blocks of data from a file. It is generally preferred for binary files but can also be used to read the text files.

C
#include <stdio.h>

int main() {
    FILE *fptr = fopen("file.txt", "r");
    if (fptr == NULL) {
        return 1;
    }

    // Read and print each block of data from the file
    char buff[100];
    size_t n;
    while ((n = fread(buff, sizeof(char), sizeof(buff) - 1, fptr)) > 0) {
      
      	// Manually have to NULL terminate buffer
        buff[n] = '\0';
        printf("%s", buff);
    }

    fclose(fptr);
    return 0;
}


Output

Hello, Geeks!
This is a test file.

Explanation: In this program, fread() is used to read raw data from the file file.txt in blocks of up to 99 bytes (or character as each character is 1 byte). The function returns the number of characters successfully read, but we manually add a null terminator (\0) to the buffer after each read to ensure the string is properly formatted for printing.



Next Article

Similar Reads