C Programming - read a file line by line with fgets and getline, implement a portable getline version
Posted on April 3, 2019 past Paul
In this article, I will show you lot how to read a text file line by line in C using the standard C office fgets and the POSIX getline function. At the end of the commodity, I will write a portable implementation of the getline office that can be used with any standard C compiler.
Reading a file line by line is a trivial problem in many programming languages, merely non in C. The standard fashion of reading a line of text in C is to utilize the fgets function, which is fine if you lot know in advance how long a line of text could be.
You can observe all the lawmaking examples and the input file at the GitHub repo for this article.
Allow'southward beginning with a simple case 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 slice from the output of the higher up program on my machine:
The lawmaking prints the content of the clamper array, as filled after every phone call to fgets, and a marker string.
If you lot scout carefully, by scrolling the above text snippet to the right, you can see that the output was truncated to 127 characters per line of text. This was expected considering our lawmaking can store an entire line from the original text file only if the line can fit within our chunk array.
What if yous need 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 end of line grapheme.
Let's beginning by creating a line buffer that will store the chunks of text, initially this will accept the same length every bit the chunk array:
Next, we are going to suspend the content of the chunk array to the terminate of the line string, until we find the cease of line character. If necessary, we'll resize the line buffer:
Delight note, that in the above code, every fourth dimension the line buffer needs to be resized its chapters is doubled.
This is the result of running the to a higher place code on my auto. For brevity, I kept just the showtime lines of output:
Y'all can run across that, this time, we can print full lines of text and not fixed length chunks like in the initial approach.
Let'southward modify the above code in gild to print the line length instead of the actual text:
This is the result of running the modified lawmaking on my machine:
In the next example, I volition bear witness you how to utilize the getline role bachelor on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, so you won't be able to hands exam this case on a Windows system. Withal, you should be able to test it if you lot are using Cygwin or Windows Subsystem for Linux.
Please note, how simple is to use POSIX's getline versus manually buffering chunks of line like in my previous example. Information technology is unfortunate that the standard C library doesn't include an equivalent part.
When you apply getline, don't forget to free the line buffer when you don't demand it anymore. As well, calling getline more than than one time volition overwrite the line buffer, make a copy of the line content if y'all need to keep it for further processing.
This is the event of running the above getline example on a Linux car:
It is interesting to annotation, that for this detail instance the getline function on Linux resizes the line buffer to a max of 960 bytes. If you lot run the aforementioned code 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 earlier, getline is not present in the C standard library. Information technology could be an interesting exercise to implement a portable version of this function. The thought here is not to implement the most performant version of getline, but rather to implement a simple replacement for non POSIX systems.
We are going to take the in a higher place 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 employ the version provided by the operating organization, which was tested by endless users and tuned for optimal performance.
The POSIX getline role has this signature:
Since ssize_t is besides a POSIX divers type, unremarkably 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 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 we found the end of line character:
Share this post
0 Response to "How to Read a File Line by Line C#"
0 Response to "How to Read a File Line by Line C#"
Postar um comentário