C Programming - read a file line by line with fgets and getline, implement a portable getline version
Posted on Apr 3, 2019 by Paul
In this article, I volition show 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 end of the article, I will write a portable implementation of the getline function that can be used with any standard C compiler.
Reading a file line past line is a trivial trouble in many programming languages, simply not in C. The standard manner of reading a line of text in C is to utilize the fgets function, which is fine if yous know in advance how long a line of text could be.
You tin can observe all the lawmaking examples and the input file at the GitHub repo for this article.
Let's offset with a unproblematic example of using fgets to read chunks from a text file. :
For testing the lawmaking I've used a elementary 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 chunk array, as filled later on every call to fgets, and a marker cord.
If you lookout carefully, by scrolling the in a higher place text snippet to the right, you can see that the output was truncated to 127 characters per line of text. This was expected because our code can shop an entire line from the original text file only if the line can fit inside our chunk assortment.
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 split up line buffer until we observe the cease of line character.
Let's start by creating a line buffer that will store the chunks of text, initially this will have the same length as the chunk array:
Adjacent, we are going to append the content of the clamper array to the terminate of the line string, until we find the end of line graphic symbol. If necessary, nosotros'll resize the line buffer:
Please note, that in the above code, every time the line buffer needs to be resized its capacity is doubled.
This is the result of running the to a higher place code on my auto. For brevity, I kept merely the offset lines of output:
You tin see that, this time, we can impress total lines of text and not stock-still length chunks like in the initial approach.
Permit'south modify the higher up lawmaking in order to print the line length instead of the actual text:
This is the consequence of running the modified code on my machine:
In the side by side example, I volition show you how to use the getline role available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't take an equivalent office, so you won't be able to easily test this example on a Windows system. However, you lot should be able to test it if you are using Cygwin or Windows Subsystem for Linux.
Please notation, how simple is to use POSIX's 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 role.
When you use getline, don't forget to free the line buffer when y'all don't need information technology anymore. Besides, calling getline more than once will overwrite the line buffer, make a copy of the line content if you need to keep it for further processing.
This is the outcome of running the in a higher place getline case on a Linux car:
Information technology is interesting to note, that for this particular instance the getline function 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. Information technology could be an interesting practice to implement a portable version of this office. The idea here is not to implement the near performant version of getline, only rather to implement a simple replacement for not POSIX systems.
Nosotros are going to accept the above example and replace the POSIX's getline version with our ain implementation, say my_getline. Plainly, if you are on a POSIX system, you should apply the version provided by the operating system, which was tested past countless users and tuned for optimal performance.
The POSIX getline office 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 part using the same approach every bit in one of the higher up examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we constitute the end of line character: