C Programming - read a file line by line with fgets and getline, implement a portable getline version
Posted on April three, 2019 by Paul
In this commodity, I volition bear witness yous how to read a text file line by line in C using the standard C function fgets and the POSIX getline function. At the end of the article, I will write a portable implementation of the getline role that can exist used with any standard C compiler.
Reading a file line by line is a trivial trouble in many programming languages, merely not in C. The standard way of reading a line of text in C is to use the fgets office, which is fine if you know in advance how long a line of text could be.
Yous can detect all the code examples and the input file at the GitHub repo for this commodity.
Let's offset with a simple instance 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 piece from the output of the above plan on my auto:
The code prints the content of the clamper assortment, as filled after every call to fgets, and a marker string.
If you picket carefully, by scrolling the to a higher place text snippet to the correct, y'all can see that the output was truncated to 127 characters per line of text. This was expected considering our lawmaking tin can store an entire line from the original text file only if the line can fit inside our chunk array.
What if y'all demand to have the unabridged 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 end of line character.
Let's start by creating a line buffer that will shop the chunks of text, initially this volition take the aforementioned length equally the chunk array:
Side by side, we are going to append the content of the chunk array to the end of the line cord, until we notice the end of line character. If necessary, we'll resize the line buffer:
Delight note, that in the above lawmaking, every time the line buffer needs to be resized its capacity is doubled.
This is the result of running the above code on my motorcar. For brevity, I kept only the get-go lines of output:
You tin see that, this time, nosotros tin can print full lines of text and not fixed length chunks like in the initial approach.
Permit's modify the to a higher place code in gild to print the line length instead of the bodily text:
This is the result of running the modified lawmaking on my machine:
In the next case, I will show you lot how to use the getline office bachelor on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, and then y'all won't be able to easily test this example on a Windows system. However, you should be able to test information technology if you are using Cygwin or Windows Subsystem for Linux.
Please note, how simple is to employ POSIX'southward getline versus manually buffering chunks of line like in my previous example. 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. Also, calling getline more than than once will overwrite the line buffer, make a copy of the line content if y'all need to keep it for further processing.
This is the result of running the above getline case on a Linux machine:
It is interesting to notation, that for this particular case the getline role 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 different ways in which getline is implemented on different Unix like systems.
As mentioned earlier, 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 not to implement the most performant version of getline, but rather to implement a elementary replacement for non POSIX systems.
We are going to have the above example and replace the POSIX's getline version with our ain implementation, say my_getline. Obviously, if you lot are on a POSIX system, y'all should utilise the version provided by the operating system, which was tested by countless users and tuned for optimal performance.
The POSIX getline part 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 nosotros are going to implement the role using the same approach as in ane 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:
Share this post
0 Response to "Function Read How Many Lines in a File C"
0 Response to "Function Read How Many Lines in a File C"
Post a Comment