How to Read a Whole Line in C
Solarian Programmer
My programming ramblings
C Programming - read a file line past line with fgets and getline, implement a portable getline version
Posted on April 3, 2019 by Paul
In this commodity, I will testify yous how to read a text file line by line in C using the standard C part fgets and the POSIX getline office. At the end of the article, I will write a portable implementation of the getline function that can be used with any standard C compiler.
Reading a file line by line is a lilliputian problem in many programming languages, but not in C. The standard way of reading a line of text in C is to utilise 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 code examples and the input file at the GitHub repo for this article.
Let's outset with a simple instance of using fgets to read chunks from a text file. :
1 #include <stdio.h> two #include <stdlib.h> iii 4 int main ( void ) { 5 FILE * fp = fopen ( "lorem.txt" , "r" ); vi if ( fp == NULL ) { 7 perror ( "Unable to open up file!" ); eight leave ( one ); 9 } x 11 char chunk [ 128 ]; 12 13 while ( fgets ( chunk , sizeof ( chunk ), fp ) != NULL ) { 14 fputs ( chunk , stdout ); 15 fputs ( "|* \n " , stdout ); // marker string used to show where the content of the chunk assortment has ended 16 } 17 18 fclose ( fp ); 19 } For testing the code I've used a unproblematic dummy file, lorem.txt. This is a piece from the output of the above program on my machine:
1 ~ $ 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 |* viii Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien |* 9 dignissim molestie. ten |* The code prints the content of the chunk array, as filled after every call to fgets, and a marker string.
If you watch carefully, by scrolling the to a higher place text snippet to the right, you can see that the output was truncated to 127 characters per line of text. This was expected because our code can shop an entire line from the original text file merely if the line tin fit within our chunk assortment.
What if y'all 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 separate line buffer until we detect the end of line graphic symbol.
Let'south kickoff by creating a line buffer that will shop the chunks of text, initially this will take the same length as the chunk array:
1 #include <stdio.h> two #include <stdlib.h> 3 #include <cord.h> 4 five int main ( void ) { half-dozen FILE * fp = fopen ( "lorem.txt" , "r" ); 7 // ... eight nine char chunk [ 128 ]; 10 xi // 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." ); xvi exit ( 1 ); 17 } 18 xix // "Empty" the cord 20 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 cord, until we discover the finish of line grapheme. If necessary, we'll resize the line buffer:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 five int main ( void ) { 6 // ... seven viii // "Empty" the string 9 line [ 0 ] = '\0' ; 10 11 while ( fgets ( clamper , sizeof ( chunk ), fp ) != NULL ) { 12 // Resize the line buffer if necessary 13 size_t len_used = strlen ( line ); 14 size_t chunk_used = strlen ( clamper ); fifteen 16 if ( len - len_used < chunk_used ) { 17 len *= 2 ; 18 if (( line = realloc ( line , len )) == Aught ) { xix perror ( "Unable to reallocate retentivity for the line buffer." ); twenty free ( line ); 21 get out ( 1 ); 22 } 23 } 24 25 // Re-create the chunk to the end of the line buffer 26 strncpy ( line + len_used , chunk , len - len_used ); 27 len_used += chunk_used ; 28 29 // Check if line contains '\north', if yes 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 free ( line ); 40 41 printf ( " \due north\north Max line size: %zd \due north " , len ); 42 } Delight notation, that in the higher up lawmaking, every time the line buffer needs to exist resized its capacity is doubled.
This is the upshot of running the to a higher place code on my machine. For brevity, I kept only the first lines of output:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 2 ~ $ ./t1 three Lorem ipsum dolor sit amet, consectetur adipiscing elit. 4 |* 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 |* ix Aliquam erat volutpat. Mauris dignissim augue ac purus placerat scelerisque. Donec eleifend ut nibh european union elementum. 10 |* You tin can see that, this time, nosotros can print full lines of text and not stock-still length chunks like in the initial arroyo.
Permit'southward modify the above code in order to print the line length instead of the bodily text:
ane // ... 2 3 int chief ( void ) { 4 // ... five vi while ( fgets ( chunk , sizeof ( chunk ), fp ) != NULL ) { 7 eight // ... 9 10 // Check if line contains '\n', if yes procedure the line of text 11 if ( line [ len_used - 1 ] == '\n' ) { 12 printf ( "line length: %zd \n " , len_used ); 13 // "Empty" the line buffer xiv line [ 0 ] = '\0' ; fifteen } 16 } 17 18 fclose ( fp ); nineteen free ( line ); xx 21 printf ( " \northward\north Max line size: %zd \due north " , len ); 22 } This is the result of running the modified code on my machine:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 2 ~ $ ./t1 iii line length: 57 4 line length: 136 5 line length: 147 half dozen line length: 114 seven line length: 112 8 line length: 95 nine line length: 62 10 line length: i xi line length: 428 12 line length: i thirteen line length: 460 fourteen line length: i fifteen line length: 834 16 line length: i 17 line length: 821 18 xix xx Max line size: 1024 In the next example, I will bear witness y'all how to use the getline function available on POSIX systems similar Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, so you won't exist able to hands test this instance on a Windows system. Nonetheless, you lot should exist able to test it if you are using Cygwin or Windows Subsystem for Linux.
ane #include <stdio.h> 2 #include <stdlib.h> three #include <string.h> 4 5 int primary ( void ) { six FILE * fp = fopen ( "lorem.txt" , "r" ); 7 if ( fp == NULL ) { 8 perror ( "Unable to open file!" ); 9 leave ( 1 ); 10 } eleven 12 // Read lines using POSIX function getline 13 // This code won't work on Windows 14 char * line = NULL ; 15 size_t len = 0 ; xvi 17 while ( getline ( & line , & len , fp ) != - 1 ) { xviii printf ( "line length: %zd \n " , strlen ( line )); 19 } xx 21 printf ( " \north\n Max line size: %zd \northward " , len ); 22 23 fclose ( fp ); 24 free ( line ); // getline volition resize the input buffer as necessary 25 // the user needs to free the memory when not needed! 26 } Please note, how simple is to use POSIX'south getline versus manually buffering chunks of line like in my previous example. It is unfortunate that the standard C library doesn't include an equivalent function.
When you use getline, don't forget to gratuitous the line buffer when yous don't demand it anymore. As well, calling getline more than than in one case volition overwrite the line buffer, brand a copy of the line content if yous demand to keep it for further processing.
This is the upshot of running the in a higher place getline example on a Linux machine:
ane ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2 2 ~ $ ./t2 iii line length: 57 4 line length: 136 5 line length: 147 6 line length: 114 7 line length: 112 eight line length: 95 nine line length: 62 10 line length: 1 11 line length: 428 12 line length: ane xiii line length: 460 fourteen line length: ane fifteen line length: 834 16 line length: 1 17 line length: 821 18 19 20 Max line size: 960 It is interesting to note, that for this item case the getline function on Linux resizes the line buffer to a max of 960 bytes. If you run the same code 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 nowadays in the C standard library. It could be an interesting exercise to implement a portable version of this function. The idea here is non to implement the nearly performant version of getline, but rather to implement a unproblematic replacement for non POSIX systems.
We are going to take the higher up example and replace the POSIX's getline version with our own implementation, say my_getline. Patently, if you are on a POSIX organisation, you should use the version provided by the operating system, which was tested by countless users and tuned for optimal functioning.
The POSIX getline role has this signature:
1 ssize_t getline ( char ** restrict lineptr , size_t * restrict n , FILE * restrict stream ); Since ssize_t is besides a POSIX defined type, commonly a 64 $.25 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 nosotros are going to implement the function using the same approach equally in 1 of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until nosotros establish the end of line character:
ane // This will only have effect on Windows with MSVC two #ifdef _MSC_VER 3 #define _CRT_SECURE_NO_WARNINGS 1 iv #define restrict __restrict five
0 Response to "How to Read a Whole Line in C"
Post a Comment