C Programming - read a file line by line with fgets and getline, implement a portable getline version
Posted on April 3, 2022 by Paul
In this article, I will bear witness you how to read a text file line past line in C using the standard C part fgets and the POSIX getline function. At the cease of the article, 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 piffling trouble in many programming languages, but non in C. The standard way of reading a line of text in C is to employ the fgets role, which is fine if you know in accelerate how long a line of text could exist.
You tin can detect all the code examples and the input file at the GitHub repo for this article.
Let's start with a uncomplicated instance of using fgets to read chunks from a text file. :
For testing the code I've used a uncomplicated dummy file, lorem.txt. This is a piece from the output of the above program on my motorcar:
The code prints the content of the chunk array, as filled subsequently every call to fgets, and a marking string.
If you watch carefully, by scrolling the above text snippet to the right, you can run into that the output was truncated to 127 characters per line of text. This was expected considering our lawmaking can store an unabridged line from the original text file only if the line can fit inside our chunk array.
What if y'all need to take the entire line of text available for farther processing and non a piece of line ? A possible solution is to re-create or concatenate chunks of text in a separate line buffer until we find the stop of line grapheme.
Let'due south offset by creating a line buffer that will store the chunks of text, initially this will take the same length equally the chunk array:
Next, we are going to append the content of the chunk array to the end of the line string, until we find the end of line character. If necessary, we'll resize the line buffer:
Please note, that in the to a higher place code, every time the line buffer needs to be resized its chapters is doubled.
This is the upshot of running the to a higher place code on my automobile. For brevity, I kept only the get-go lines of output:
You lot can see that, this time, we can print full lines of text and not fixed length chunks like in the initial arroyo.
Let's modify the to a higher place lawmaking in society to print the line length instead of the actual text:
This is the result of running the modified lawmaking on my motorcar:
In the next example, I volition prove you how to use the getline office available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent office, so y'all won't be able to easily test this example on a Windows system. Still, you lot should be able to exam 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 similar in my previous example. Information technology is unfortunate that the standard C library doesn't include an equivalent function.
When yous utilize getline, don't forget to gratuitous the line buffer when yous don't need it anymore. Also, calling getline more than once will overwrite the line buffer, make a copy of the line content if you demand to proceed it for farther processing.
This is the effect of running the to a higher place getline example on a Linux auto:
It is interesting to annotation, that for this particular case the getline function on Linux resizes the line buffer to a max of 960 bytes. If you run the aforementioned lawmaking on macOS the line buffer is resized to 1024 bytes. This is due to the dissimilar ways in which getline is implemented on different Unix like systems.
As mentioned before, getline is not nowadays in the C standard library. It could be an interesting do to implement a portable version of this function. The idea here is not to implement the most performant version of getline, but rather to implement a uncomplicated replacement for not POSIX systems.
We are going to have the above example and replace the POSIX's getline version with our own implementation, say my_getline. Obviously, if you are on a POSIX organisation, you lot should use the version provided by the operating organisation, which was tested by endless users and tuned for optimal performance.
The POSIX getline function has this signature:
Since ssize_t is also a POSIX defined blazon, unremarkably a 64 bits signed integer, this is how we are going to declare our version:
In principle nosotros are going to implement the function using the same arroyo as in i of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we establish the end of line character:
Share this post
0 Response to "Using Fgets in C to Read Multiple Lines"
0 Response to "Using Fgets in C to Read Multiple Lines"
ارسال یک نظر