Using Fgets in C to Read Multiple Lines
Solarian Developer
My programming ramblings
C Programming - read a file line by line with fgets and getline, implement a portable getline version
Posted on April 3, 2022 by Paul
In this article, I will bear witness you how to read a text file line past line in C using the standard C part fgets and the POSIX getline function. At the cease of the article, I volition 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 piffling trouble in many programming languages, but non in C. The standard way of reading a line of text in C is to employ the fgets role, which is fine if you know in accelerate how long a line of text could exist.
You tin can detect all the code examples and the input file at the GitHub repo for this article.
Let's start with a uncomplicated instance of using fgets to read chunks from a text file. :
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main ( void ) { 5 FILE * fp = fopen ( "lorem.txt" , "r" ); 6 if ( fp == NULL ) { 7 perror ( "Unable to open up file!" ); 8 exit ( 1 ); 9 } 10 11 char clamper [ 128 ]; 12 13 while ( fgets ( chunk , sizeof ( chunk ), fp ) != Nil ) { fourteen fputs ( clamper , stdout ); 15 fputs ( "|* \northward " , stdout ); // mark cord used to show where the content of the chunk array has ended xvi } 17 eighteen fclose ( fp ); 19 }
For testing the code I've used a uncomplicated dummy file, lorem.txt. This is a piece from the output of the above program on my motorcar:
ane ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0 2 ~ $ ./t0 3 Lorem ipsum dolor sit down amet, consectetur adipiscing elit. 4 |* 5 Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare cond|* 6 imentum. 7 |* 8 Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien |* ix dignissim molestie. ten |*
The code prints the content of the chunk array, as filled subsequently every call to fgets, and a marking string.
If you watch carefully, by scrolling the above text snippet to the right, you can run into that the output was truncated to 127 characters per line of text. This was expected considering our lawmaking can store an unabridged line from the original text file only if the line can fit inside our chunk array.
What if y'all need to take the entire line of text available for farther processing and non a piece of line ? A possible solution is to re-create or concatenate chunks of text in a separate line buffer until we find the stop of line grapheme.
Let'due south offset by creating a line buffer that will store the chunks of text, initially this will take the same length equally the chunk array:
1 #include <stdio.h> two #include <stdlib.h> 3 #include <string.h> 4 five int main ( void ) { half-dozen FILE * fp = fopen ( "lorem.txt" , "r" ); 7 // ... eight ix char chunk [ 128 ]; 10 eleven // Store the chunks of text into a line buffer 12 size_t len = sizeof ( chunk ); 13 char * line = malloc ( len ); 14 if ( line == NULL ) { 15 perror ( "Unable to allocate retentiveness for the line buffer." ); 16 exit ( 1 ); 17 } 18 nineteen // "Empty" the string twenty line [ 0 ] = '\0' ; 21 22 // ... 23 24 }
Next, we are going to append the content of the chunk array to the end of the line string, until we find the end of line character. If necessary, we'll resize the line buffer:
one #include <stdio.h> 2 #include <stdlib.h> 3 #include <cord.h> 4 5 int main ( void ) { six // ... 7 viii // "Empty" the string 9 line [ 0 ] = '\0' ; 10 11 while ( fgets ( clamper , sizeof ( clamper ), fp ) != NULL ) { 12 // Resize the line buffer if necessary xiii size_t len_used = strlen ( line ); 14 size_t chunk_used = strlen ( chunk ); 15 16 if ( len - len_used < chunk_used ) { 17 len *= 2 ; 18 if (( line = realloc ( line , len )) == Aught ) { 19 perror ( "Unable to reallocate memory for the line buffer." ); 20 free ( line ); 21 exit ( 1 ); 22 } 23 } 24 25 // Re-create the chunk to the stop of the line buffer 26 strncpy ( line + len_used , chunk , len - len_used ); 27 len_used += chunk_used ; 28 29 // Check if line contains '\n', if yep process the line of text 30 if ( line [ len_used - 1 ] == '\n' ) { 31 fputs ( line , stdout ); 32 fputs ( "|* \n " , stdout ); 33 // "Empty" the line buffer 34 line [ 0 ] = '\0' ; 35 } 36 } 37 38 fclose ( fp ); 39 complimentary ( line ); forty 41 printf ( " \due north\n Max line size: %zd \n " , len ); 42 }
Please note, that in the to a higher place code, every time the line buffer needs to be resized its chapters is doubled.
This is the upshot of running the to a higher place code on my automobile. For brevity, I kept only the get-go lines of output:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 ii ~ $ ./t1 3 Lorem ipsum dolor sit down amet, consectetur adipiscing elit. 4 |* five Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare condimentum. vi |* 7 Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien dignissim molestie. viii |* 9 Aliquam erat volutpat. Mauris dignissim augue ac purus placerat scelerisque. Donec eleifend ut nibh european union elementum. 10 |*
You lot can see that, this time, we can print full lines of text and not fixed length chunks like in the initial arroyo.
Let's modify the to a higher place lawmaking in society to print the line length instead of the actual text:
1 // ... 2 3 int main ( void ) { iv // ... 5 six while ( fgets ( clamper , sizeof ( chunk ), fp ) != Zilch ) { seven eight // ... nine 10 // Bank check if line contains '\n', if aye procedure the line of text 11 if ( line [ len_used - ane ] == '\n' ) { 12 printf ( "line length: %zd \n " , len_used ); 13 // "Empty" the line buffer fourteen line [ 0 ] = '\0' ; fifteen } 16 } 17 xviii fclose ( fp ); xix free ( line ); 20 21 printf ( " \due north\north Max line size: %zd \n " , len ); 22 }
This is the result of running the modified lawmaking on my motorcar:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 ii ~ $ ./t1 three line length: 57 four line length: 136 5 line length: 147 6 line length: 114 7 line length: 112 viii line length: 95 nine line length: 62 10 line length: one xi line length: 428 12 line length: 1 13 line length: 460 14 line length: i 15 line length: 834 16 line length: i 17 line length: 821 xviii 19 twenty Max line size: 1024
In the next example, I volition prove you how to use the getline office available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent office, so y'all won't be able to easily test this example on a Windows system. Still, you lot should be able to exam it if you are using Cygwin or Windows Subsystem for Linux.
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main ( void ) { half dozen FILE * fp = fopen ( "lorem.txt" , "r" ); 7 if ( fp == Null ) { 8 perror ( "Unable to open file!" ); ix exit ( ane ); 10 } xi 12 // Read lines using POSIX part getline thirteen // This code won't piece of work on Windows 14 char * line = Null ; fifteen size_t len = 0 ; 16 17 while ( getline ( & line , & len , fp ) != - 1 ) { eighteen printf ( "line length: %zd \n " , strlen ( line )); 19 } 20 21 printf ( " \due north\n Max line size: %zd \n " , len ); 22 23 fclose ( fp ); 24 costless ( line ); // getline will resize the input buffer every bit necessary 25 // the user needs to complimentary the retentivity when not needed! 26 }
Please notation, how simple is to use POSIX's getline versus manually buffering chunks of line similar in my previous example. Information technology is unfortunate that the standard C library doesn't include an equivalent function.
When yous utilize getline, don't forget to gratuitous the line buffer when yous don't need it anymore. Also, calling getline more than once will overwrite the line buffer, make a copy of the line content if you demand to proceed it for farther processing.
This is the effect of running the to a higher place getline example on a Linux auto:
i ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2 ii ~ $ ./t2 3 line length: 57 4 line length: 136 5 line length: 147 6 line length: 114 7 line length: 112 8 line length: 95 9 line length: 62 ten line length: 1 11 line length: 428 12 line length: 1 13 line length: 460 14 line length: ane 15 line length: 834 16 line length: i 17 line length: 821 18 nineteen twenty Max line size: 960
It is interesting to annotation, that for this particular case the getline function on Linux resizes the line buffer to a max of 960 bytes. If you run the aforementioned lawmaking on macOS the line buffer is resized to 1024 bytes. This is due to the dissimilar ways in which getline is implemented on different Unix like systems.
As mentioned before, getline is not nowadays in the C standard library. It could be an interesting do 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 uncomplicated replacement for not POSIX systems.
We are going to have the above example and replace the POSIX's getline version with our own implementation, say my_getline. Obviously, if you are on a POSIX organisation, you lot should use the version provided by the operating organisation, which was tested by endless users and tuned for optimal performance.
The POSIX getline function has this signature:
1 ssize_t getline ( char ** restrict lineptr , size_t * restrict n , FILE * restrict stream );
Since ssize_t is also a POSIX defined blazon, unremarkably a 64 bits signed integer, this is how we are going to declare our version:
one int64_t my_getline ( char ** restrict line , size_t * restrict len , FILE * restrict fp );
In principle nosotros are going to implement the function using the same arroyo as in i of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we establish the end of line character:
i // This will only have effect on Windows with MSVC 2 #ifdef _MSC_VER 3 #define _CRT_SECURE_NO_WARNINGS 1 four #define restrict __restrict 5
0 Response to "Using Fgets in C to Read Multiple Lines"
ارسال یک نظر