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 commodity, I volition bear witness you how to read a text file line by line in C using the standard C role fgets and the POSIX getline office. At the terminate of the commodity, I will write a portable implementation of the getline part that can be used with whatever standard C compiler.
Reading a file line by line is a trivial trouble in many programming languages, but not in C. The standard way 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 be.
You can find all the code examples and the input file at the GitHub repo for this article.
Let's commencement with a simple 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 piece from the output of the higher up program on my machine:
The code prints the content of the clamper array, as filled after every call to fgets, and a marking string.
If yous picket advisedly, by scrolling the above text snippet to the correct, you lot tin can see that the output was truncated to 127 characters per line of text. This was expected considering our code tin can store an entire line from the original text file only if the line tin fit inside our clamper array.
What if you 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 finish of line character.
Permit's offset by creating a line buffer that will store the chunks of text, initially this will have the same length as the clamper array:
Next, we are going to append the content of the clamper assortment to the terminate of the line string, until nosotros find the terminate of line character. If necessary, we'll resize the line buffer:
Please annotation, that in the to a higher place code, every time the line buffer needs to exist resized its capacity is doubled.
This is the outcome of running the higher up code on my car. For brevity, I kept only the showtime lines of output:
You can see that, this time, we tin can print total lines of text and not fixed length chunks similar in the initial approach.
Let's modify the above code in order to print the line length instead of the actual text:
This is the event of running the modified code on my motorcar:
In the next instance, I will prove yous how to utilise the getline function available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't take an equivalent part, and then you won't be able to easily test this example on a Windows system. However, you lot should be able to test it if you are using Cygwin or Windows Subsystem for Linux.
Please notation, how uncomplicated is to utilize POSIX'south 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 office.
When you employ getline, don't forget to free the line buffer when you don't demand it anymore. Also, calling getline more once will overwrite the line buffer, brand a copy of the line content if you demand to keep it for farther processing.
This is the result of running the to a higher place getline case on a Linux machine:
It is interesting to note, that for this particular example the getline office on Linux resizes the line buffer to a max of 960 bytes. If yous run the same code 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 like systems.
As mentioned before, getline is not present in the C standard library. It could exist an interesting practice to implement a portable version of this function. 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 have the to a higher place example and supplant the POSIX's getline version with our ain implementation, say my_getline. Obviously, if you are on a POSIX system, you lot should apply the version provided by the operating organisation, which was tested by countless users and tuned for optimal functioning.
The POSIX getline function has this signature:
Since ssize_t is also a POSIX defined blazon, commonly 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 arroyo 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 institute the end of line character:
Bagikan Artikel ini
Belum ada Komentar untuk "How to Read Entire Line in C++ From Stdin"
Belum ada Komentar untuk "How to Read Entire Line in C++ From Stdin"
Posting Komentar