C Programming - read a file line by line with fgets and getline, implement a portable getline version
Posted on Apr 3, 2022 by Paul
In this commodity, I will bear witness y'all how to read a text file line by line in C using the standard C function fgets and the POSIX getline office. At the end of the commodity, I volition write a portable implementation of the getline function that tin be used with any standard C compiler.
Reading a file line by line is a piddling problem in many programming languages, only non in C. The standard way of reading a line of text in C is to use the fgets function, which is fine if y'all know in accelerate 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.
Let's outset with a simple example of using fgets to read chunks from a text file. :
For testing the lawmaking I've used a simple dummy file, lorem.txt. This is a piece from the output of the in a higher place program on my auto:
The code prints the content of the chunk array, equally filled later every phone call to fgets, and a mark string.
If you sentinel carefully, by scrolling the higher up text snippet to the right, y'all can 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 can fit inside our chunk array.
What if you demand to have the entire line of text bachelor for further processing and not a piece of line ? A possible solution is to copy or concatenate chunks of text in a split line buffer until we find the end of line character.
Let's start by creating a line buffer that will store the chunks of text, initially this volition have the same length as the chunk assortment:
Next, we are going to append the content of the chunk array to the stop of the line string, until nosotros find the terminate 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 upshot of running the above code on my machine. For brevity, I kept simply the first lines of output:
Y'all can see that, this time, nosotros can print full lines of text and non fixed length chunks like in the initial approach.
Let's alter the above code in guild to print the line length instead of the actual text:
This is the result of running the modified lawmaking on my machine:
In the side by side case, I volition show you lot how to apply the getline office available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't take an equivalent function, so you won't be able to easily test this instance on a Windows arrangement. Even so, you should be able to exam it if you are using Cygwin or Windows Subsystem for Linux.
Please note, how unproblematic is to utilize POSIX'due south 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 y'all apply getline, don't forget to free the line buffer when you don't need it anymore. Also, calling getline more than than one time will overwrite the line buffer, make a copy of the line content if yous demand to keep it for further processing.
This is the result of running the above getline case on a Linux machine:
Information technology 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 lawmaking on macOS the line buffer is resized to 1024 bytes. This is due to the different ways in which getline is implemented on unlike Unix similar 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 thought hither is non to implement the virtually performant version of getline, but rather to implement a uncomplicated replacement for non POSIX systems.
Nosotros are going to accept the in a higher place example and supercede the POSIX'south getline version with our ain implementation, say my_getline. Obviously, if you are on a POSIX organisation, you lot should apply the version provided past the operating organization, which was tested past countless users and tuned for optimal operation.
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 approach every bit in ane of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until nosotros found the end of line graphic symbol:
0 Response to "Using Getline to Read a File in C"
Post a Comment