C How to Read File Line by Line
Solarian Programmer
My programming ramblings
C Programming - read a file line past line with fgets and getline, implement a portable getline version
Posted on Apr 3, 2019 by Paul
In this commodity, 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 role. At the terminate of the commodity, 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 trivial problem in many programming languages, simply not in C. The standard manner of reading a line of text in C is to use the fgets function, which is fine if y'all 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.
Permit'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 ) { five FILE * fp = fopen ( "lorem.txt" , "r" ); 6 if ( fp == Zilch ) { 7 perror ( "Unable to open up file!" ); 8 exit ( i ); ix } x 11 char chunk [ 128 ]; 12 xiii while ( fgets ( clamper , sizeof ( chunk ), fp ) != NULL ) { 14 fputs ( chunk , stdout ); xv fputs ( "|* \north " , stdout ); // mark string used to show where the content of the clamper assortment has ended 16 } 17 eighteen fclose ( fp ); 19 }
For testing the code I've used a simple dummy file, lorem.txt. This is a slice 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 amet, consectetur adipiscing elit. four |* five 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 |* nine dignissim molestie. x |*
The code prints the content of the clamper array, as filled afterward every call to fgets, and a marker string.
If you picket carefully, by scrolling the to a higher place text snippet to the right, you tin can encounter that the output was truncated to 127 characters per line of text. This was expected considering our lawmaking tin shop an entire line from the original text file only if the line tin fit inside our chunk assortment.
What if you demand to accept the entire line of text available for farther 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 find the stop of line character.
Let'south showtime by creating a line buffer that will store the chunks of text, initially this will accept the same length as the chunk assortment:
1 #include <stdio.h> 2 #include <stdlib.h> three #include <cord.h> 4 5 int primary ( void ) { 6 FILE * fp = fopen ( "lorem.txt" , "r" ); 7 // ... 8 9 char clamper [ 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 ) { fifteen perror ( "Unable to allocate memory for the line buffer." ); 16 go out ( 1 ); 17 } eighteen 19 // "Empty" the string 20 line [ 0 ] = '\0' ; 21 22 // ... 23 24 }
Next, we are going to append the content of the clamper array to the end of the line string, until nosotros observe the finish of line character. If necessary, we'll resize the line buffer:
1 #include <stdio.h> ii #include <stdlib.h> 3 #include <string.h> 4 5 int primary ( void ) { six // ... 7 eight // "Empty" the string 9 line [ 0 ] = '\0' ; 10 eleven while ( fgets ( chunk , sizeof ( chunk ), fp ) != Nil ) { 12 // Resize the line buffer if necessary 13 size_t len_used = strlen ( line ); fourteen size_t chunk_used = strlen ( chunk ); 15 sixteen if ( len - len_used < chunk_used ) { 17 len *= 2 ; 18 if (( line = realloc ( line , len )) == NULL ) { nineteen perror ( "Unable to reallocate memory for the line buffer." ); 20 complimentary ( line ); 21 go out ( 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 '\n', if yeah process the line of text thirty if ( line [ len_used - 1 ] == '\northward' ) { 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 ); xl 41 printf ( " \n\n Max line size: %zd \n " , len ); 42 }
Please note, that in the above code, every fourth dimension the line buffer needs to be resized its chapters is doubled.
This is the result of running the above lawmaking on my machine. For brevity, I kept only the beginning lines of output:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 two ~ $ ./t1 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 condimentum. six |* 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 tin run into that, this fourth dimension, we tin can print full lines of text and not fixed length chunks like in the initial approach.
Permit'south modify the above code in gild to print the line length instead of the actual text:
1 // ... 2 3 int main ( void ) { four // ... 5 6 while ( fgets ( chunk , sizeof ( chunk ), fp ) != Null ) { vii viii // ... ix 10 // Check if line contains '\northward', if yes process the line of text 11 if ( line [ len_used - i ] == '\n' ) { 12 printf ( "line length: %zd \n " , len_used ); thirteen // "Empty" the line buffer 14 line [ 0 ] = '\0' ; xv } 16 } 17 18 fclose ( fp ); xix free ( line ); twenty 21 printf ( " \n\n Max line size: %zd \n " , len ); 22 }
This is the upshot of running the modified code on my machine:
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 8 line length: 95 ix line length: 62 10 line length: i 11 line length: 428 12 line length: 1 xiii line length: 460 14 line length: ane 15 line length: 834 16 line length: i 17 line length: 821 18 19 twenty Max line size: 1024
In the next example, I volition prove you how to utilise the getline function bachelor on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't accept an equivalent function, then y'all won't be able to easily test this instance on a Windows system. However, you should be able to examination it if you lot are using Cygwin or Windows Subsystem for Linux.
1 #include <stdio.h> ii #include <stdlib.h> iii #include <cord.h> iv 5 int main ( void ) { 6 FILE * fp = fopen ( "lorem.txt" , "r" ); 7 if ( fp == NULL ) { 8 perror ( "Unable to open up file!" ); 9 leave ( 1 ); 10 } 11 12 // Read lines using POSIX office getline 13 // This code won't piece of work on Windows 14 char * line = Zero ; 15 size_t len = 0 ; 16 17 while ( getline ( & line , & len , fp ) != - ane ) { 18 printf ( "line length: %zd \n " , strlen ( line )); 19 } twenty 21 printf ( " \n\n Max line size: %zd \northward " , len ); 22 23 fclose ( fp ); 24 free ( line ); // getline volition resize the input buffer every bit necessary 25 // the user needs to complimentary the retentiveness when not needed! 26 }
Please note, how simple is to utilize POSIX's getline versus manually buffering chunks of line similar in my previous example. It is unfortunate that the standard C library doesn't include an equivalent function.
When yous use getline, don't forget to free the line buffer when you don't need it anymore. Also, calling getline more than than once will overwrite the line buffer, make a re-create of the line content if you demand to keep it for further processing.
This is the result of running the above getline instance on a Linux motorcar:
ane ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2 2 ~ $ ./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 10 line length: 1 11 line length: 428 12 line length: 1 xiii line length: 460 fourteen line length: 1 15 line length: 834 16 line length: one 17 line length: 821 eighteen xix 20 Max line size: 960
It is interesting to note, that for this item case the getline role on Linux resizes the line buffer to a max of 960 bytes. If you run the aforementioned code on macOS the line buffer is resized to 1024 bytes. This is due to the different 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 exist an interesting exercise to implement a portable version of this function. The idea here is not to implement the nearly performant version of getline, only rather to implement a simple replacement for not POSIX systems.
We are going to accept the to a higher place example and supplant the POSIX'south getline version with our own implementation, say my_getline. Obviously, if yous are on a POSIX system, yous should utilise the version provided past the operating organisation, which was tested by countless users and tuned for optimal performance.
The POSIX getline role has this signature:
one ssize_t getline ( char ** restrict lineptr , size_t * restrict northward , 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 we are going to implement the role using the aforementioned approach as in one of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until we found the end of line character:
1 // This will merely have effect on Windows with MSVC 2 #ifdef _MSC_VER 3 #define _CRT_SECURE_NO_WARNINGS one 4 #ascertain restrict __restrict 5