C – Read Text File

C Read Text File – There are many ways in which you can read a text file. Some of them are.

  1. Read entire contents of a file.
  2. Read text file line by line.
  3. Read text file character by character.

In this tutorial, we will go through each of these processes, with example programs.

C – Read entire contents of text File

To read entire contents of text file to a string in C programming, follow these steps.

  1. Open the text file in read mode, using fopen(). If file pointer is null, return 1, else continue.
  2. Get the length of text file using fseek() and ftell() functions.
  3. Create a string, (character array) and allocate memory using the length of text file. If memory is not successful, return 1, else continue.
  4. Read contents of text file into the string using fread() function.
  5. Close the text file using fclose() function.
  6. The string contains the whole text of text file. You may use it as required, while we just print it to the console.

C Program

#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE    *textfile;
    char    *text;
    long    numbytes;
    
    textfile = fopen("readme.txt", "r");
    if(textfile == NULL)
        return 1;
    
    fseek(textfile, 0L, SEEK_END);
    numbytes = ftell(textfile);
    fseek(textfile, 0L, SEEK_SET);	

    text = (char*)calloc(numbytes, sizeof(char));	
    if(text == NULL)
        return 1;

    fread(text, sizeof(char), numbytes, textfile);
    fclose(textfile);

    printf(text);

    return 0;
}

Output

PS D:\workspace\c> .\main.exe
Hello!
welcome to www.tutorialkart.com!
Welcome to C Tutorial.
ADVERTISEMENT

C – Read Text File Line by Line

To read contents of text file line by line in C programming, follow these steps.

  1. Open the text file in read mode, using fopen(). If file pointer is null, return 1, else continue.
  2. Using fgets(), read next line into a string. fgets() returns string or EOF. You can use while loop to read line by line using fgets() function. During each iteration of while loop, you will get contents of the text file line by line.
  3. Close the text file using fclose() function.

C Program

#include <stdio.h>

#define MAX_LINE_LENGTH 1000

int main() {
    FILE    *textfile;
    char    line[MAX_LINE_LENGTH];
    
    textfile = fopen("readme.txt", "r");
    if(textfile == NULL)
        return 1;
    
    while(fgets(line, MAX_LINE_LENGTH, textfile)){
        printf(line);
    }
    
    fclose(textfile);
    return 0;
}

Output

PS D:\workspace\c> .\main.exe
Hello!
welcome to www.tutorialkart.com!
Welcome to C Tutorial.

C – Read Text File Character by Character

To read contents of text file character by character in C programming, follow these steps.

  1. Open the text file in read mode, using fopen(). If file pointer is null, return 1, else continue.
  2. Using fgetc() function, read next character from text file into a string. fgetc() returns character read from the file. You can use while loop to read file stream, character by character, until you get EOF.
  3. Close the text file using fclose() function.

C Program

#include <stdio.h>

#define MAX_LINE_LENGTH 1000

int main() {
    FILE    *textfile;
    char    ch;
    
    textfile = fopen("readme.txt", "r");
    if(textfile == NULL)
        return 1;
    
    while((ch = fgetc(textfile))!=EOF) {
        putchar(ch);
    }
    
    fclose(textfile);
    return 0;
}

Output

PS D:\workspace\c> .\main.exe
Hello!
welcome to www.tutorialkart.com!
Welcome to C Tutorial.

Conclusion

In this C Tutorial, we learned how to read text file in C programming language.