C Open a File and Read Line by Line
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 commodity, I will show you how to read a text file line past line in C using the standard C function fgets and the POSIX getline function. At the end of the article, I will write a portable implementation of the getline office that can be used with any standard C compiler.
Reading a file line past line is a piddling problem in many programming languages, but not in C. The standard fashion 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.
Let's start with a simple instance of using fgets to read chunks from a text file. :
1 #include <stdio.h> ii #include <stdlib.h> 3 4 int chief ( void ) { 5 FILE * fp = fopen ( "lorem.txt" , "r" ); 6 if ( fp == NULL ) { 7 perror ( "Unable to open file!" ); 8 exit ( ane ); 9 } 10 11 char clamper [ 128 ]; 12 xiii while ( fgets ( clamper , sizeof ( chunk ), fp ) != Zilch ) { fourteen fputs ( chunk , stdout ); 15 fputs ( "|* \n " , stdout ); // marker cord used to show where the content of the chunk assortment has ended 16 } 17 xviii fclose ( fp ); xix } 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 machine:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0 2 ~ $ ./t0 3 Lorem ipsum dolor sit amet, consectetur adipiscing elit. 4 |* v Fusce dignissim facilisis ligula consectetur hendrerit. Vestibulum porttitor aliquam luctus. Nam pharetra lorem vel ornare cond|* half dozen imentum. vii |* 8 Praesent et nunc at libero vulputate convallis. Cras egestas nunc vitae eros vehicula hendrerit. Pellentesque in est et sapien |* 9 dignissim molestie. 10 |* The lawmaking prints the content of the chunk assortment, equally filled afterwards every telephone call to fgets, and a marker string.
If you sentry advisedly, by scrolling the to a higher place text snippet to the right, you tin can see that the output was truncated to 127 characters per line of text. This was expected because our lawmaking can store an entire line from the original text file only if the line can fit inside our chunk assortment.
What if you demand to have the entire line of text available for further processing and non a slice of line ? A possible solution is to re-create or concatenate chunks of text in a split up line buffer until nosotros detect the end of line character.
Let'southward beginning past creating a line buffer that will store the chunks of text, initially this volition take the same length as the chunk assortment:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <cord.h> iv 5 int main ( void ) { 6 FILE * fp = fopen ( "lorem.txt" , "r" ); 7 // ... 8 nine char chunk [ 128 ]; 10 11 // Store the chunks of text into a line buffer 12 size_t len = sizeof ( chunk ); 13 char * line = malloc ( len ); fourteen if ( line == NULL ) { xv perror ( "Unable to classify retentivity for the line buffer." ); sixteen exit ( i ); 17 } 18 19 // "Empty" the string xx line [ 0 ] = '\0' ; 21 22 // ... 23 24 } Next, we are going to append the content of the chunk array to the cease of the line string, until we find the cease of line character. If necessary, we'll resize the line buffer:
1 #include <stdio.h> 2 #include <stdlib.h> three #include <string.h> 4 v int master ( void ) { 6 // ... 7 8 // "Empty" the string 9 line [ 0 ] = '\0' ; 10 11 while ( fgets ( chunk , sizeof ( chunk ), fp ) != Nix ) { 12 // Resize the line buffer if necessary 13 size_t len_used = strlen ( line ); 14 size_t chunk_used = strlen ( chunk ); 15 16 if ( len - len_used < chunk_used ) { 17 len *= 2 ; eighteen if (( line = realloc ( line , len )) == Naught ) { 19 perror ( "Unable to reallocate memory for the line buffer." ); 20 free ( line ); 21 go out ( 1 ); 22 } 23 } 24 25 // Copy the clamper to the finish of the line buffer 26 strncpy ( line + len_used , clamper , len - len_used ); 27 len_used += chunk_used ; 28 29 // Bank check if line contains '\northward', if yep process the line of text 30 if ( line [ len_used - i ] == '\due 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 costless ( line ); xl 41 printf ( " \n\n Max line size: %zd \northward " , len ); 42 } Please notation, that in the above lawmaking, every time the line buffer needs to exist resized its chapters is doubled.
This is the outcome of running the to a higher place code on my motorcar. For brevity, I kept but the commencement lines of output:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 2 ~ $ ./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 ac purus placerat scelerisque. Donec eleifend ut nibh european union elementum. x |* You can see that, this time, nosotros can impress full lines of text and not fixed length chunks like in the initial approach.
Let'southward modify the above code in order to print the line length instead of the actual text:
one // ... ii 3 int main ( void ) { four // ... 5 6 while ( fgets ( clamper , sizeof ( chunk ), fp ) != Aught ) { 7 viii // ... 9 x // Check if line contains '\n', if aye procedure the line of text 11 if ( line [ len_used - 1 ] == '\n' ) { 12 printf ( "line length: %zd \north " , len_used ); 13 // "Empty" the line buffer 14 line [ 0 ] = '\0' ; xv } 16 } 17 18 fclose ( fp ); 19 free ( line ); 20 21 printf ( " \north\north Max line size: %zd \n " , len ); 22 } This is the result of running the modified code on my motorcar:
one ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 2 ~ $ ./t1 iii 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: 1 eleven line length: 428 12 line length: ane thirteen line length: 460 fourteen line length: 1 xv line length: 834 16 line length: 1 17 line length: 821 18 nineteen 20 Max line size: 1024 In the next instance, I will testify you how to employ the getline part available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent role, and then you lot won't be able to easily examination this case on a Windows system. Yet, y'all should be able to test it if you are using Cygwin or Windows Subsystem for Linux.
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> four 5 int chief ( void ) { 6 FILE * fp = fopen ( "lorem.txt" , "r" ); vii if ( fp == NULL ) { 8 perror ( "Unable to open file!" ); ix exit ( 1 ); x } 11 12 // Read lines using POSIX function getline thirteen // This lawmaking won't piece of work on Windows fourteen char * line = Zilch ; xv size_t len = 0 ; xvi 17 while ( getline ( & line , & len , fp ) != - i ) { 18 printf ( "line length: %zd \north " , strlen ( line )); 19 } 20 21 printf ( " \n\northward 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 complimentary the memory when non needed! 26 } Please notation, how uncomplicated is to use POSIX'south getline versus manually buffering chunks of line like in my previous example. Information technology 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 you don't need information technology anymore. Also, calling getline more than once will overwrite the line buffer, brand a re-create of the line content if y'all need to go on information technology for further processing.
This is the upshot of running the higher up getline example on a Linux automobile:
1 ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2 ii ~ $ ./t2 3 line length: 57 4 line length: 136 v line length: 147 6 line length: 114 7 line length: 112 eight line length: 95 9 line length: 62 10 line length: 1 eleven line length: 428 12 line length: 1 13 line length: 460 14 line length: 1 fifteen line length: 834 16 line length: i 17 line length: 821 eighteen 19 twenty Max line size: 960 Information technology is interesting to annotation, that for this particular instance the getline function on Linux resizes the line buffer to a max of 960 bytes. If y'all run the aforementioned code 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.
Every bit mentioned earlier, getline is non present in the C standard library. Information technology could be an interesting exercise to implement a portable version of this office. The idea hither is not to implement the well-nigh performant version of getline, but rather to implement a unproblematic replacement for non POSIX systems.
Nosotros are going to take the above example and replace the POSIX's getline version with our own implementation, say my_getline. Obviously, if you are on a POSIX system, you should use the version provided by the operating system, which was tested by countless users and tuned for optimal functioning.
The POSIX getline function has this signature:
i ssize_t getline ( char ** restrict lineptr , size_t * restrict n , FILE * restrict stream ); Since ssize_t is too a POSIX defined type, usually 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 function using the same arroyo as in one 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 graphic symbol:
1 // This volition merely take effect on Windows with MSVC 2 #ifdef _MSC_VER 3 #define _CRT_SECURE_NO_WARNINGS 1 4 #ascertain restrict __restrict 5
0 Response to "C Open a File and Read Line by Line"
Post a Comment