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 show you how to read a text file line by line in C using the standard C office fgets and the POSIX getline office. At the end of the article, I will write a portable implementation of the getline function that can be used with any standard C compiler.
Reading a file line past line is a trivial problem in many programming languages, but not in C. The standard way of reading a line of text in C is to utilise the fgets function, which is fine if you lot know in accelerate how long a line of text could exist.
You tin observe all the lawmaking examples and the input file at the GitHub repo for this article.
Permit'south start with a uncomplicated example of using fgets to read chunks from a text file. :
For testing the code I've used a elementary dummy file, lorem.txt. This is a slice from the output of the in a higher place programme on my machine:
The code prints the content of the chunk assortment, as filled after every call to fgets, and a marker string.
If yous sentry carefully, by scrolling the above text snippet to the correct, you can see that the output was truncated to 127 characters per line of text. This was expected because our code can shop an unabridged line from the original text file but if the line tin fit within our chunk array.
What if yous need to have 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 character.
Permit'due south start past creating a line buffer that will store the chunks of text, initially this volition have the same length as the chunk array:
Next, nosotros are going to append the content of the chunk array to the end of the line string, until we discover the end of line graphic symbol. If necessary, we'll resize the line buffer:
Please note, that in the to a higher place lawmaking, every fourth dimension the line buffer needs to be resized its capacity is doubled.
This is the result of running the to a higher place code on my machine. For brevity, I kept only the outset lines of output:
You can see that, this fourth dimension, we tin can print full lines of text and non fixed length chunks like in the initial approach.
Let's alter the above lawmaking in club to impress the line length instead of the actual text:
This is the result of running the modified code on my automobile:
In the next instance, I will show yous how to use the getline function available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent role, and then you won't be able to hands test this case on a Windows arrangement. However, you should be able to test information technology if you are using Cygwin or Windows Subsystem for Linux.
Please notation, how simple is to use POSIX'due south getline versus manually buffering chunks of line like in my previous case. Information technology is unfortunate that the standard C library doesn't include an equivalent function.
When yous utilize getline, don't forget to complimentary the line buffer when you don't need it anymore. Also, calling getline more than once will overwrite the line buffer, brand a copy of the line content if you demand to go on information technology for further processing.
This is the consequence of running the in a higher place getline example on a Linux machine:
It is interesting to notation, that for this item case the getline office on Linux resizes the line buffer to a max of 960 bytes. If you run the aforementioned code on macOS the line buffer is resized to 1024 bytes. This is due to the unlike ways in which getline is implemented on different Unix like systems.
As mentioned before, getline is not present in the C standard library. It could be an interesting practise to implement a portable version of this part. The idea 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 above instance and replace the POSIX's getline version with our own implementation, say my_getline. Plainly, if you are on a POSIX arrangement, you should use the version provided by the operating system, which was tested by countless users and tuned for optimal functioning.
The POSIX getline function has this signature:
Since ssize_t is besides a POSIX defined type, usually a 64 $.25 signed integer, this is how we are going to declare our version:
In principle we are going to implement the role using the same arroyo as in 1 of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we plant the end of line grapheme:
0 Response to "Fgets Read One Line at a Time"
Post a Comment