read a text file line by line in C

problem

You have a text file, for example a CSV file that you wish to read using the C programming language.  In this post we will see how to accomplish this.

SOLUTION

Let’s read the file line by line using the filesystem’s functions. The plan is to simply specify the filename we wish to read, open it, read it line by line (print it on the screen) and finally close it to release memory.

				
					#include <stdio.h>

#define MAX_LINE_LENGTH 100

int main(int argc, char *argv[]) {

    FILE *file;
    char line[MAX_LINE_LENGTH];
     
    file = fopen("some_text.txt", "r"); //open for reading

    if(file == NULL)
        return 1; // can't be opened
     
    while(fgets(line, MAX_LINE_LENGTH, file)){
        printf("%s", line);
    }
     
    fclose(file); //release memory

    return 0;
}
				
			

The code above declares a char array (aka string) of 100 items that will be used to read each line. We assume that each line will have less than 100 characters. The code opens the file, reads it line by line, prints every line and then closes it to avoid any memory leak.

Below we can see how it can be run.

running

Running the code above will assume we have a text file saved in the same directory with the code and the filename is some_text.txt. For this example we can assume a text file with the following contents:

				
					line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
end.

				
			
compile

For compiling the source code, the GCC is used as shown below:

				
					gcc read_file_line_by_line.c
				
			
output

To run it, using the terminal on a Mac, simply:

				
					./a.out
				
			
The output:

conclusion

In this post we saw how to simply open an existing text file and read it line by line using the legendary C programming language.

Share it!

Facebook
Twitter
LinkedIn
Reddit
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

Google Analytics Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.

HotJar Cookies

We use Hotjar in order to better understand our users’ needs and to optimize this service and experience. Hotjar is a technology service that helps us better understand our users’ experience (e.g. how much time they spend on which pages, which links they choose to click, what users do and don’t like, etc.) and this enables us to build and maintain our service with user feedback. Hotjar uses cookies and other technologies to collect data on our users’ behavior and their devices. This includes a device's IP address (processed during your session and stored in a de-identified form), device screen size, device type (unique device identifiers), browser information, geographic location (country only), and the preferred language used to display our website. Hotjar stores this information on our behalf in a pseudonymized user profile. Hotjar is contractually forbidden to sell any of the data collected on our behalf.

For further details, please see the ‘about Hotjar’ section of Hotjar’s support site.