C Read Line From Text One at a Time Fgets
Solarian Programmer
My programming ramblings
C Programming - read a file line by line with fgets and getline, implement a portable getline version
Posted on April iii, 2019 by Paul
In this article, I volition 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 terminate 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 trivial problem in many programming languages, but non in C. The standard way of reading a line of text in C is to employ the fgets function, which is fine if you lot know in accelerate how long a line of text could be.
Yous can find all the lawmaking examples and the input file at the GitHub repo for this article.
Let's first with a uncomplicated example of using fgets to read chunks from a text file. :
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int principal ( void ) { 5 FILE * fp = fopen ( "lorem.txt" , "r" ); 6 if ( fp == Goose egg ) { 7 perror ( "Unable to open file!" ); 8 exit ( one ); ix } 10 11 char chunk [ 128 ]; 12 xiii while ( fgets ( clamper , sizeof ( chunk ), fp ) != Goose egg ) { 14 fputs ( chunk , stdout ); xv fputs ( "|* \northward " , stdout ); // marker string used to show where the content of the clamper array has ended xvi } 17 18 fclose ( fp ); 19 }
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 machine:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0 2 ~ $ ./t0 3 Lorem ipsum dolor sit amet, consectetur adipiscing elit. iv |* 5 Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare cond|* half dozen imentum. 7 |* 8 Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien |* 9 dignissim molestie. x |*
The code prints the content of the clamper assortment, as filled after every call to fgets, and a marker string.
If yous watch carefully, by scrolling the in a higher place text snippet to the correct, you can meet that the output was truncated to 127 characters per line of text. This was expected considering our lawmaking can shop an unabridged line from the original text file only if the line can fit inside our chunk 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 dissever line buffer until we find the finish of line character.
Allow's start by creating a line buffer that will store the chunks of text, initially this will take the same length as the chunk assortment:
one #include <stdio.h> ii #include <stdlib.h> iii #include <string.h> 4 five int chief ( void ) { 6 FILE * fp = fopen ( "lorem.txt" , "r" ); 7 // ... 8 9 char chunk [ 128 ]; x eleven // Store the chunks of text into a line buffer 12 size_t len = sizeof ( chunk ); xiii char * line = malloc ( len ); xiv if ( line == Nothing ) { 15 perror ( "Unable to allocate memory for the line buffer." ); sixteen exit ( one ); 17 } 18 19 // "Empty" the string 20 line [ 0 ] = '\0' ; 21 22 // ... 23 24 }
Next, we are going to suspend the content of the clamper array to the end of the line string, until nosotros notice the stop of line grapheme. If necessary, we'll resize the line buffer:
ane #include <stdio.h> two #include <stdlib.h> three #include <cord.h> 4 v int main ( void ) { six // ... 7 8 // "Empty" the string 9 line [ 0 ] = '\0' ; ten 11 while ( fgets ( chunk , sizeof ( chunk ), fp ) != Aught ) { 12 // Resize the line buffer if necessary xiii size_t len_used = strlen ( line ); 14 size_t chunk_used = strlen ( chunk ); fifteen xvi if ( len - len_used < chunk_used ) { 17 len *= 2 ; eighteen if (( line = realloc ( line , len )) == Aught ) { 19 perror ( "Unable to reallocate memory for the line buffer." ); twenty gratis ( line ); 21 exit ( 1 ); 22 } 23 } 24 25 // Re-create the chunk to the end of the line buffer 26 strncpy ( line + len_used , clamper , len - len_used ); 27 len_used += chunk_used ; 28 29 // Check if line contains '\due north', if yep process the line of text thirty if ( line [ len_used - 1 ] == '\north' ) { 31 fputs ( line , stdout ); 32 fputs ( "|* \northward " , stdout ); 33 // "Empty" the line buffer 34 line [ 0 ] = '\0' ; 35 } 36 } 37 38 fclose ( fp ); 39 free ( line ); 40 41 printf ( " \n\north Max line size: %zd \n " , len ); 42 }
Please note, that in the above code, every time the line buffer needs to exist resized its chapters is doubled.
This is the issue of running the in a higher place code on my automobile. For brevity, I kept only the kickoff lines of output:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 two ~ $ ./t1 3 Lorem ipsum dolor sit amet, consectetur adipiscing elit. four |* 5 Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare condimentum. 6 |* 7 Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien dignissim molestie. 8 |* 9 Aliquam erat volutpat. Mauris dignissim augue air-conditioning purus placerat scelerisque. Donec eleifend ut nibh eu elementum. 10 |*
Y'all tin run across that, this time, nosotros can impress full lines of text and not fixed length chunks like in the initial arroyo.
Let's modify the above code in social club to print the line length instead of the actual text:
ane // ... 2 3 int principal ( void ) { 4 // ... 5 6 while ( fgets ( chunk , sizeof ( chunk ), fp ) != NULL ) { 7 8 // ... 9 x // Check if line contains '\north', if yes process the line of text 11 if ( line [ len_used - one ] == '\n' ) { 12 printf ( "line length: %zd \n " , len_used ); 13 // "Empty" the line buffer 14 line [ 0 ] = '\0' ; 15 } 16 } 17 18 fclose ( fp ); nineteen free ( line ); 20 21 printf ( " \n\northward Max line size: %zd \north " , len ); 22 }
This is the result of running the modified lawmaking on my auto:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 2 ~ $ ./t1 3 line length: 57 4 line length: 136 5 line length: 147 6 line length: 114 7 line length: 112 viii line length: 95 9 line length: 62 10 line length: i 11 line length: 428 12 line length: 1 13 line length: 460 14 line length: 1 15 line length: 834 xvi line length: ane 17 line length: 821 18 19 20 Max line size: 1024
In the adjacent example, I will testify you how to use the getline function available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, so yous won't exist able to easily exam this example on a Windows organisation. However, you should be able to test information technology 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 ) { 6 FILE * fp = fopen ( "lorem.txt" , "r" ); 7 if ( fp == NULL ) { 8 perror ( "Unable to open file!" ); 9 get out ( 1 ); 10 } xi 12 // Read lines using POSIX role getline 13 // This code won't work on Windows 14 char * line = NULL ; fifteen size_t len = 0 ; sixteen 17 while ( getline ( & line , & len , fp ) != - 1 ) { 18 printf ( "line length: %zd \north " , strlen ( line )); 19 } twenty 21 printf ( " \n\north Max line size: %zd \n " , len ); 22 23 fclose ( fp ); 24 free ( line ); // getline will resize the input buffer as necessary 25 // the user needs to free the memory when non needed! 26 }
Please note, how elementary is to apply POSIX'southward getline versus manually buffering chunks of line like in my previous instance. It is unfortunate that the standard C library doesn't include an equivalent part.
When you use getline, don't forget to free the line buffer when yous don't need information technology anymore. Also, calling getline more than once will overwrite the line buffer, make a re-create of the line content if you need to keep information technology for farther processing.
This is the result of running the above getline example on a Linux auto:
ane ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2 2 ~ $ ./t2 three line length: 57 4 line length: 136 five line length: 147 half dozen line length: 114 vii line length: 112 eight line length: 95 9 line length: 62 10 line length: 1 11 line length: 428 12 line length: ane 13 line length: 460 fourteen line length: one 15 line length: 834 16 line length: ane 17 line length: 821 xviii 19 twenty Max line size: 960
It is interesting to note, that for this item instance the getline function on Linux resizes the line buffer to a max of 960 bytes. If y'all 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 dissimilar Unix like systems.
As 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 hither is not to implement the most performant version of getline, just rather to implement a simple replacement for non POSIX systems.
We are going to take the in a higher place example and supervene upon the POSIX's getline version with our ain implementation, say my_getline. Obviously, if y'all are on a POSIX system, you lot should use the version provided past the operating system, which was tested by countless users and tuned for optimal performance.
The POSIX getline function has this signature:
ane ssize_t getline ( char ** restrict lineptr , size_t * restrict due north , FILE * restrict stream );
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:
1 int64_t my_getline ( char ** restrict line , size_t * restrict len , FILE * restrict fp );
In principle we are going to implement the function using the aforementioned arroyo every bit in one of the higher up examples, where I've divers a line buffer and kept copying chunks of text in the buffer until we constitute the cease of line character:
1 // This will only take upshot on Windows with MSVC 2 #ifdef _MSC_VER 3 #define _CRT_SECURE_NO_WARNINGS 1 four #define restrict __restrict five
0 Response to "C Read Line From Text One at a Time Fgets"
Post a Comment