C Programming - read a file line by line with fgets and getline, implement a portable getline version
Posted on April 3, 2019 by Paul
In this article, I will evidence yous how to read a text file line by line in C using the standard C part fgets and the POSIX getline function. At the end of the article, I will write a portable implementation of the getline function that can be used with whatsoever standard C compiler.
Reading a file line by line is a trivial problem in many programming languages, but not in C. The standard fashion of reading a line of text in C is to use the fgets office, which is fine if you know in accelerate how long a line of text could be.
Y'all can detect all the code examples and the input file at the GitHub repo for this commodity.
Permit's start with a simple 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 slice from the output of the above programme on my machine:
The lawmaking prints the content of the chunk array, as filled after every call to fgets, and a marker string.
If you watch carefully, by scrolling the above text snippet to the right, yous tin see that the output was truncated to 127 characters per line of text. This was expected because our code can store an entire line from the original text file merely if the line tin can fit inside our chunk assortment.
What if y'all demand to accept 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 separate line buffer until we find the terminate of line character.
Allow'south beginning past creating a line buffer that will store the chunks of text, initially this will accept the same length as the chunk array:
Adjacent, nosotros are going to suspend the content of the chunk array to the finish of the line string, until we find the end of line character. If necessary, nosotros'll resize the line buffer:
Please notation, that in the to a higher place code, every time the line buffer needs to exist resized its capacity is doubled.
This is the result of running the above code on my machine. For brevity, I kept only the first lines of output:
You can meet that, this time, nosotros tin can print full lines of text and not fixed length chunks like in the initial approach.
Let's modify the above code in order to print the line length instead of the actual text:
This is the result of running the modified code on my machine:
In the next example, I will evidence you how to utilise the getline part available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent part, then yous won't be able to hands test this instance on a Windows system. However, y'all should be able to exam it if you are using Cygwin or Windows Subsystem for Linux.
Please note, how uncomplicated is to utilize POSIX'south getline versus manually buffering chunks of line similar in my previous case. It is unfortunate that the standard C library doesn't include an equivalent function.
When you use getline, don't forget to free the line buffer when you don't need it anymore. Likewise, calling getline more than once will overwrite the line buffer, brand a copy of the line content if you need to go along it for farther processing.
This is the result of running the above getline example on a Linux machine:
It is interesting to note, that for this particular case the getline function on Linux resizes the line buffer to a max of 960 bytes. If you run the same 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.
Equally mentioned earlier, getline is non present in the C standard library. It could be an interesting exercise to implement a portable version of this function. The idea hither is not to implement the most performant version of getline, only rather to implement a simple replacement for non POSIX systems.
We are going to take the above example and replace the POSIX's getline version with our own implementation, say my_getline. Obviously, if you are on a POSIX system, you should use the version provided by the operating system, which was tested by countless users and tuned for optimal performance.
The POSIX getline office has this signature:
Since ssize_t is likewise a POSIX divers type, normally a 64 $.25 signed integer, this is how we are going to declare our version:
In principle we are going to implement the function using the same 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 nosotros plant the end of line character:
0 Response to "Read in Lines From Text File C"
Post a Comment