C Programming - read a file line past line with fgets and getline, implement a portable getline version
Posted on April three, 2019 by Paul
In this commodity, I volition show you 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 article, I will write a portable implementation of the getline role that can exist 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 mode of reading a line of text in C is to utilize the fgets function, which is fine if you know in advance how long a line of text could be.
You lot can find all the code examples and the input file at the GitHub repo for this article.
Let's starting time with a simple 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 piece from the output of the higher up plan on my automobile:
The code prints the content of the chunk array, equally filled subsequently every call to fgets, and a mark cord.
If you picket carefully, by scrolling the above text snippet to the correct, you lot tin can run into that the output was truncated to 127 characters per line of text. This was expected considering our code can store an entire line from the original text file only if the line tin can fit inside our clamper assortment.
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 we find the end of line grapheme.
Permit'south beginning by creating a line buffer that volition store the chunks of text, initially this volition have the same length equally the clamper array:
Next, nosotros are going to suspend the content of the chunk array to the end of the line string, until we observe the terminate of line character. If necessary, we'll resize the line buffer:
Please annotation, that in the in a higher place code, every fourth dimension the line buffer needs to be resized its chapters is doubled.
This is the result of running the in a higher place code on my auto. For brevity, I kept only the beginning lines of output:
Y'all can see that, this time, we tin can print full lines of text and not stock-still length chunks like in the initial arroyo.
Let's modify the to a higher place code in order to impress the line length instead of the bodily text:
This is the issue of running the modified code on my car:
In the adjacent example, I will show y'all how to use the getline office available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent role, so you won't exist able to easily test this example on a Windows system. Nevertheless, you should exist able to test information technology if you are using Cygwin or Windows Subsystem for Linux.
Please notation, how uncomplicated is to use POSIX'southward getline versus manually buffering chunks of line like in my previous instance. Information technology is unfortunate that the standard C library doesn't include an equivalent function.
When you apply getline, don't forget to costless the line buffer when you don't need it anymore. Also, calling getline more than than one time will overwrite the line buffer, brand a copy of the line content if you lot need to proceed it for farther processing.
This is the event of running the higher up getline example on a Linux automobile:
Information technology is interesting to note, that for this particular case the getline role 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 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 exercise to implement a portable version of this role. The thought hither is not to implement the well-nigh performant version of getline, merely rather to implement a simple replacement for non POSIX systems.
We are going to take the above example and supersede the POSIX'due south getline version with our own implementation, say my_getline. Obviously, 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 function has this signature:
Since ssize_t is also a POSIX defined type, usually a 64 bits signed integer, this is how we are going to declare our version:
In principle nosotros are going to implement the function using the aforementioned approach as in one of the higher up examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we plant the end of line character:
Share this post
0 Response to "How to Read an Entire Line From a File in C"
0 Response to "How to Read an Entire Line From a File in C"
Post a Comment