C Programming - read a file line past line with fgets and getline, implement a portable getline version
Posted on Apr 3, 2019 by Paul
In this commodity, 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 role. At the terminate of the commodity, I volition 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, simply not in C. The standard manner of reading a line of text in C is to use the fgets function, which is fine if y'all know in advance how long a line of text could be.
You can find all the code examples and the input file at the GitHub repo for this article.
Permit's start with a uncomplicated instance 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 slice from the output of the above program on my machine:
The code prints the content of the clamper array, as filled afterward every call to fgets, and a marker string.
If you picket carefully, by scrolling the to a higher place text snippet to the right, you tin can encounter that the output was truncated to 127 characters per line of text. This was expected considering our lawmaking tin shop an entire line from the original text file only if the line tin fit inside our chunk assortment.
What if you demand to accept the entire line of text available for farther processing and not a piece of line ? A possible solution is to copy or concatenate chunks of text in a separate line buffer until we find the stop of line character.
Let'south showtime by creating a line buffer that will store the chunks of text, initially this will accept the same length as the chunk assortment:
Next, we are going to append the content of the clamper array to the end of the line string, until nosotros observe the finish of line character. If necessary, we'll resize the line buffer:
Please note, that in the above code, every fourth dimension the line buffer needs to be resized its chapters is doubled.
This is the result of running the above lawmaking on my machine. For brevity, I kept only the beginning lines of output:
You tin run into that, this fourth dimension, we tin can print full lines of text and not fixed length chunks like in the initial approach.
Permit'south modify the above code in gild to print the line length instead of the actual text:
This is the upshot of running the modified code on my machine:
In the next example, I volition prove you how to utilise the getline function bachelor on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't accept an equivalent function, then y'all won't be able to easily test this instance on a Windows system. However, you should be able to examination it if you lot are using Cygwin or Windows Subsystem for Linux.
Please note, how simple is to utilize POSIX's getline versus manually buffering chunks of line similar in my previous example. It is unfortunate that the standard C library doesn't include an equivalent function.
When yous use getline, don't forget to free the line buffer when you don't need it anymore. Also, calling getline more than than once will overwrite the line buffer, make a re-create of the line content if you demand to keep it for further processing.
This is the result of running the above getline instance on a Linux motorcar:
It is interesting to note, that for this item case the getline role on Linux resizes the line buffer to a max of 960 bytes. If you run the aforementioned code on macOS the line buffer is resized to 1024 bytes. This is due to the different ways in which getline is implemented on different 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 here is not to implement the nearly performant version of getline, only rather to implement a simple replacement for not POSIX systems.
We are going to accept the to a higher place example and supplant the POSIX'south getline version with our own implementation, say my_getline. Obviously, if yous are on a POSIX system, yous should utilise the version provided past the operating organisation, which was tested by countless users and tuned for optimal performance.
The POSIX getline role has this signature:
Since ssize_t is besides a POSIX defined type, commonly a 64 $.25 signed integer, this is how we are going to declare our version:
In principle we are going to implement the role using the aforementioned approach as in one of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we found the end of line character: