C Programming - read a file line by line with fgets and getline, implement a portable getline version
Posted on April iii, 2019 by Paul
In this article, I volition testify you how to read a text file line by line in C using the standard C function fgets and the POSIX getline function. At the terminate of the article, I will write a portable implementation of the getline function that can exist used with any standard C compiler.
Reading a file line by line is a trivial problem in many programming languages, but non in C. The standard way of reading a line of text in C is to employ the fgets function, which is fine if you lot know in accelerate how long a line of text could be.
Yous can find all the lawmaking examples and the input file at the GitHub repo for this article.
Let's first with a uncomplicated example of using fgets to read chunks from a text file. :
For testing the code I've used a simple dummy file, lorem.txt. This is a piece from the output of the above plan on my machine:
The code prints the content of the clamper assortment, as filled after every call to fgets, and a marker string.
If yous watch carefully, by scrolling the in a higher place text snippet to the correct, you can meet that the output was truncated to 127 characters per line of text. This was expected considering our lawmaking can shop an unabridged line from the original text file only if the line can fit inside our chunk array.
What if you need to have the entire line of text available for further processing and not a piece of line ? A possible solution is to copy or concatenate chunks of text in a dissever line buffer until we find the finish of line character.
Allow's start by creating a line buffer that will store the chunks of text, initially this will take the same length as the chunk assortment:
Next, we are going to suspend the content of the clamper array to the end of the line string, until nosotros notice the stop of line grapheme. If necessary, we'll resize the line buffer:
Please note, that in the above code, every time the line buffer needs to exist resized its chapters is doubled.
This is the issue of running the in a higher place code on my automobile. For brevity, I kept only the kickoff lines of output:
Y'all tin run across that, this time, nosotros can impress full lines of text and not fixed length chunks like in the initial arroyo.
Let's modify the above code in social club to print the line length instead of the actual text:
This is the result of running the modified lawmaking on my auto:
In the adjacent example, I will testify you how to use the getline function available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, so yous won't exist able to easily exam this example on a Windows organisation. However, you should be able to test information technology if you are using Cygwin or Windows Subsystem for Linux.
Please note, how elementary is to apply POSIX'southward getline versus manually buffering chunks of line like in my previous instance. It is unfortunate that the standard C library doesn't include an equivalent part.
When you use getline, don't forget to free the line buffer when yous don't need information technology anymore. Also, calling getline more than once will overwrite the line buffer, make a re-create of the line content if you need to keep information technology for farther processing.
This is the result of running the above getline example on a Linux auto:
It is interesting to note, that for this item instance the getline function on Linux resizes the line buffer to a max of 960 bytes. If y'all run the same code on macOS the line buffer is resized to 1024 bytes. This is due to the unlike ways in which getline is implemented on dissimilar Unix like systems.
As mentioned before, getline is not present in the C standard library. It could exist an interesting exercise to implement a portable version of this function. The idea hither is not to implement the most performant version of getline, just rather to implement a simple replacement for non POSIX systems.
We are going to take the in a higher place example and supervene upon the POSIX's getline version with our ain implementation, say my_getline. Obviously, if y'all are on a POSIX system, you lot should use the version provided past the operating system, which was tested by countless users and tuned for optimal performance.
The POSIX getline function has this signature:
Since ssize_t is also a POSIX defined type, usually a 64 bits signed integer, this is how we are going to declare our version:
In principle we are going to implement the function using the aforementioned arroyo every bit in one of the higher up examples, where I've divers a line buffer and kept copying chunks of text in the buffer until we constitute the cease of line character:
Share this post
0 Response to "C Read Line From Text One at a Time Fgets"
0 Response to "C Read Line From Text One at a Time Fgets"
Post a Comment