C – Read Text File
C Read Text File – There are many ways in which you can read a text file. Some of them are.
- Read entire contents of a file.
- Read text file line by line.
- 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.
- Open the text file in read mode, using fopen(). If file pointer is null, return 1, else continue.
- Get the length of text file using fseek() and ftell() functions.
- Create a string, (character array) and allocate memory using the length of text file. If memory is not successful, return 1, else continue.
- Read contents of text file into the string using fread() function.
- Close the text file using fclose() function.
- 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
</>
Copy
#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
</>
Copy
PS D:\workspace\c> .\main.exe
Hello!
welcome to www.tutorialkart.com!
Welcome to C Tutorial.
C – Read Text File Line by Line
To read contents of text file line by line in C programming, follow these steps.
- Open the text file in read mode, using fopen(). If file pointer is null, return 1, else continue.
- 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.
- Close the text file using fclose() function.
C Program
</>
Copy
#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
</>
Copy
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.
- Open the text file in read mode, using fopen(). If file pointer is null, return 1, else continue.
- 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.
- Close the text file using fclose() function.
C Program
</>
Copy
#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
</>
Copy
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.