C Programming - read a file line past line with fgets and getline, implement a portable getline version
Posted on Apr 3, 2019 past Paul
In this article, I will prove you lot how to read a text file line by line in C using the standard C role fgets and the POSIX getline role. 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 past line is a picayune problem 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.
Allow'south start with a simple instance of using fgets to read chunks from a text file. :
For testing the lawmaking I've used a simple dummy file, lorem.txt. This is a piece from the output of the above program on my motorcar:
The lawmaking prints the content of the chunk array, every bit filled after every call to fgets, and a marker string.
If you watch carefully, by scrolling the higher up text snippet to the right, you tin see that the output was truncated to 127 characters per line of text. This was expected because our lawmaking can store an unabridged line from the original text file only if the line tin can fit within our chunk array.
What if yous demand to have the entire line of text available 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 nosotros find the terminate of line character.
Allow's beginning by creating a line buffer that volition shop the chunks of text, initially this will take the same length as the chunk array:
Next, we are going to suspend the content of the chunk array to the terminate of the line cord, until we observe the end of line character. If necessary, we'll resize the line buffer:
Please note, that in the above code, every fourth dimension the line buffer needs to be resized its capacity is doubled.
This is the consequence of running the above code on my machine. For brevity, I kept only the first lines of output:
Y'all can see that, this fourth dimension, nosotros can print full lines of text and not fixed length chunks similar in the initial approach.
Permit's modify the above code in order to print 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 will show you how to use the getline function bachelor on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, so you won't be able to easily test this example on a Windows system. Withal, yous should be able to exam it if yous 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 you employ getline, don't forget to gratis the line buffer when you don't need it anymore. Also, calling getline more than than once will overwrite the line buffer, make a copy of the line content if you need to go along it for further processing.
This is the issue of running the above getline example on a Linux machine:
It is interesting to note, that for this particular case the getline part on Linux resizes the line buffer to a max of 960 bytes. If you lot run the same 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.
Equally mentioned before, getline is not present in the C standard library. It could exist an interesting exercise 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.
Nosotros are going to have the above example and replace the POSIX's getline version with our own implementation, say my_getline. Patently, if you are on a POSIX organization, you lot should utilise the version provided by the operating arrangement, which was tested by countless users and tuned for optimal performance.
The POSIX getline function has this signature:
Since ssize_t is besides a POSIX defined blazon, normally 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 approach every bit in i of the to a higher place examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we institute the end of line grapheme:
Share this post
0 Response to "How to Use Fgets to Read Whole File"
0 Response to "How to Use Fgets to Read Whole File"
Post a Comment