C Programming - read a file line past line with fgets and getline, implement a portable getline version
Posted on April 3, 2019 past Paul
In this commodity, I will testify you how to read a text file line by line in C using the standard C function fgets and the POSIX getline function. At the stop of the article, I will write a portable implementation of the getline office that tin be used with whatsoever standard C compiler.
Reading a file line past line is a lilliputian trouble in many programming languages, but not in C. The standard fashion of reading a line of text in C is to apply 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 commodity.
Permit's start 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 plan on my automobile:
The lawmaking prints the content of the chunk assortment, as filled after every telephone call to fgets, and a marker cord.
If you watch carefully, by scrolling the above text snippet to the correct, y'all tin run into that the output was truncated to 127 characters per line of text. This was expected because our lawmaking can store an entire line from the original text file just if the line can 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 split line buffer until nosotros detect the end of line character.
Let's start by creating a line buffer that will store the chunks of text, initially this will accept the aforementioned length equally the chunk assortment:
Next, we are going to append the content of the chunk array to the terminate of the line string, until nosotros observe the cease of line grapheme. If necessary, we'll resize the line buffer:
Please note, that in the above code, every time the line buffer needs to be resized its capacity is doubled.
This is the result of running the above code on my car. For brevity, I kept only the first lines of output:
You can meet that, this time, nosotros can print full lines of text and not stock-still length chunks similar in the initial approach.
Let'south modify the above code in order to impress the line length instead of the actual text:
This is the result of running the modified code on my machine:
In the next example, I volition testify yous how to use the getline function bachelor on POSIX systems similar Linux, Unix and macOS. Microsoft Visual Studio doesn't accept an equivalent function, so you won't be able to easily test this example on a Windows organization. However, you should be able to test it if yous are using Cygwin or Windows Subsystem for Linux.
Delight annotation, how simple is to use 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 role.
When you use getline, don't forget to costless the line buffer when you don't demand it anymore. Also, calling getline more than once volition overwrite the line buffer, make a copy of the line content if you need to keep it for farther processing.
This is the result of running the higher up getline example on a Linux car:
It is interesting to note, that for this detail case the getline office on Linux resizes the line buffer to a max of 960 bytes. If yous run the aforementioned code on macOS the line buffer is resized to 1024 bytes. This is due to the different means in which getline is implemented on different Unix like systems.
Every bit mentioned before, getline is non present in the C standard library. It could be an interesting practise to implement a portable version of this role. The thought here is not to implement the about performant version of getline, but rather to implement a unproblematic replacement for non POSIX systems.
We are going to take the above example and replace the POSIX'southward getline version with our own implementation, say my_getline. Apparently, if you are on a POSIX system, you lot should use the version provided by the operating system, which was tested by countless users and tuned for optimal performance.
The POSIX getline function has this signature:
Since ssize_t is also a POSIX divers type, unremarkably a 64 $.25 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 every bit 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:
0 Response to "C Read Each Line in a File"
Post a Comment