C Programming - read a file line by line with fgets and getline, implement a portable getline version
Posted on Apr 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 function fgets and the POSIX getline function. At the end of the article, I will write a portable implementation of the getline function that can exist used with any standard C compiler.
Reading a file line by line is a piddling trouble in many programming languages, but not in C. The standard mode of reading a line of text in C is to use the fgets function, which is fine if y'all know in advance how long a line of text could be.
You tin can find all the lawmaking examples and the input file at the GitHub repo for this article.
Let'southward first with a simple instance 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 piece from the output of the above plan on my auto:
The lawmaking prints the content of the chunk assortment, as filled after every call to fgets, and a marking string.
If you lookout carefully, by scrolling the above text snippet to the correct, you tin can run into that the output was truncated to 127 characters per line of text. This was expected because our code can store an unabridged line from the original text file just if the line can fit within our chunk assortment.
What if you lot 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 carve up line buffer until we find the finish of line character.
Let's start by creating a line buffer that volition store the chunks of text, initially this will have the same length equally the chunk assortment:
Next, we are going to append the content of the chunk array to the finish of the line string, until we find the end 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 lawmaking on my machine. For brevity, I kept only the kickoff lines of output:
Y'all tin can see that, this time, we can print total lines of text and not stock-still length chunks like in the initial approach.
Let'south alter the above code in order to impress the line length instead of the bodily text:
This is the issue of running the modified code on my motorcar:
In the next example, I will prove yous how to employ the getline role available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't take an equivalent function, so you won't be able to hands test this example on a Windows system. However, you should exist able to test it if you 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 case. Information technology is unfortunate that the standard C library doesn't include an equivalent function.
When yous use getline, don't forget to costless the line buffer when y'all don't need it anymore. Too, calling getline more than than once volition overwrite the line buffer, brand a copy of the line content if y'all need to continue information technology for further processing.
This is the event of running the above getline example on a Linux motorcar:
It is interesting to notation, that for this particular case the getline role on Linux resizes the line buffer to a max of 960 bytes. If you run the same lawmaking on macOS the line buffer is resized to 1024 bytes. This is due to the different ways in which getline is implemented on dissimilar Unix similar systems.
As mentioned earlier, getline is non present in the C standard library. It could be an interesting practise to implement a portable version of this function. The thought here is non to implement the near performant version of getline, but rather to implement a elementary replacement for non POSIX systems.
Nosotros are going to accept the to a higher place instance and replace the POSIX's getline version with our own implementation, say my_getline. Manifestly, if you are on a POSIX system, you should use the version provided by the operating system, which was tested by countless users and tuned for optimal performance.
The POSIX getline office has this signature:
Since ssize_t is also a POSIX divers blazon, unremarkably a 64 $.25 signed integer, this is how we are going to declare our version:
In principle nosotros are going to implement the office using the same arroyo every bit in one of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until nosotros found the end of line grapheme:
0 Response to "Using Fgets in C to Read a File"
Post a Comment