C Programming - read a file line past line with fgets and getline, implement a portable getline version
Posted on Apr 3, 2019 by Paul
In this article, I will show y'all how to read a text file line past line in C using the standard C function fgets and the POSIX getline function. At the end of the commodity, I will write a portable implementation of the getline part that can be used with any standard C compiler.
Reading a file line past line is a lilliputian problem in many programming languages, simply not in C. The standard mode of reading a line of text in C is to use the fgets function, which is fine if you know in advance how long a line of text could exist.
You tin can detect all the lawmaking examples and the input file at the GitHub repo for this article.
Let's showtime with a unproblematic example of using fgets to read chunks from a text file. :
For testing the code I've used a simple dummy file, lorem.txt. This is a slice from the output of the above program on my machine:
The lawmaking prints the content of the chunk array, as filled after every call to fgets, and a mark string.
If you lookout man carefully, by scrolling the above text snippet to the correct, yous can meet that the output was truncated to 127 characters per line of text. This was expected because our code tin can shop an unabridged line from the original text file only if the line tin can fit within our chunk assortment.
What if you demand to accept the entire line of text bachelor for further processing and not a slice of line ? A possible solution is to copy or concatenate chunks of text in a separate line buffer until we notice the end of line character.
Let'south start past creating a line buffer that will store the chunks of text, initially this will take the same length as the chunk array:
Next, we are going to append the content of the chunk array to the cease of the line string, until we find the end of line graphic symbol. 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 chapters is doubled.
This is the result of running the to a higher place code on my machine. For brevity, I kept only the offset lines of output:
You tin run across that, this time, we tin can impress full lines of text and not stock-still length chunks like in the initial approach.
Let's modify the above code in order to print the line length instead of the bodily text:
This is the result of running the modified code on my machine:
In the next instance, I volition show y'all how to employ the getline function available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't take an equivalent function, and so you won't be able to easily exam this example on a Windows system. However, you should be able to exam it if you lot are using Cygwin or Windows Subsystem for Linux.
Delight note, how simple is to use POSIX's getline versus manually buffering chunks of line like in my previous instance. It is unfortunate that the standard C library doesn't include an equivalent role.
When you use getline, don't forget to costless the line buffer when yous don't need information technology anymore. Besides, calling getline more than once will overwrite the line buffer, make a copy of the line content if y'all need to go on it for further processing.
This is the result of running the above getline instance on a Linux auto:
It is interesting to notation, 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 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 non present in the C standard library. It could exist an interesting exercise to implement a portable version of this part. The idea here is non to implement the about performant version of getline, simply rather to implement a elementary replacement for not POSIX systems.
Nosotros are going to have the higher up example and replace the POSIX's getline version with our ain implementation, say my_getline. Obviously, if you are on a POSIX system, you lot should utilise the version provided by the operating organization, which was tested by countless users and tuned for optimal performance.
The POSIX getline office has this signature:
Since ssize_t is as well 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 same arroyo as in one of the in a higher place examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we found the end of line grapheme:
Share this post
0 Response to "C++ Read File Line by Line With Delimiter"
0 Response to "C++ Read File Line by Line With Delimiter"
Post a Comment