Read Every Line of a File 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 three, 2019 past Paul
In this article, I volition evidence you how to read a text file line past line in C using the standard C role fgets and the POSIX getline role. At the end of the article, I will write a portable implementation of the getline role that can be used with any standard C compiler.
Reading a file line by line is a trivial problem in many programming languages, but not in C. The standard manner 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 exist.
Yous can notice all the code examples and the input file at the GitHub repo for this article.
Let'south start with a simple instance of using fgets to read chunks from a text file. :
1 #include <stdio.h> 2 #include <stdlib.h> iii 4 int main ( void ) { five FILE * fp = fopen ( "lorem.txt" , "r" ); 6 if ( fp == NULL ) { seven perror ( "Unable to open file!" ); 8 exit ( 1 ); 9 } 10 eleven char chunk [ 128 ]; 12 13 while ( fgets ( chunk , sizeof ( chunk ), fp ) != NULL ) { 14 fputs ( clamper , stdout ); 15 fputs ( "|* \northward " , stdout ); // marking cord used to show where the content of the chunk assortment has concluded 16 } 17 18 fclose ( fp ); nineteen }
For testing the lawmaking I've used a unproblematic dummy file, lorem.txt. This is a piece from the output of the above program on my motorcar:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0 two ~ $ ./t0 3 Lorem ipsum dolor sit amet, consectetur adipiscing elit. 4 |* five 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 |* 9 dignissim molestie. 10 |*
The code prints the content of the chunk array, equally filled after every call to fgets, and a mark cord.
If you watch carefully, by scrolling the in a higher place text snippet to the right, y'all can see that the output was truncated to 127 characters per line of text. This was expected considering our code can store an unabridged line from the original text file only if the line tin can fit within our chunk array.
What if you need to have the entire line of text bachelor 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 find the cease of line grapheme.
Allow's commencement by creating a line buffer that volition shop the chunks of text, initially this will take the same length as the chunk array:
ane #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> iv five int main ( void ) { vi FILE * fp = fopen ( "lorem.txt" , "r" ); 7 // ... 8 9 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 ); 14 if ( line == Nada ) { 15 perror ( "Unable to allocate memory for the line buffer." ); sixteen exit ( 1 ); 17 } eighteen 19 // "Empty" the string xx line [ 0 ] = '\0' ; 21 22 // ... 23 24 }
Side by side, nosotros 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:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <cord.h> 4 five int main ( void ) { six // ... 7 8 // "Empty" the string ix line [ 0 ] = '\0' ; 10 eleven while ( fgets ( chunk , sizeof ( clamper ), fp ) != Nada ) { 12 // Resize the line buffer if necessary 13 size_t len_used = strlen ( line ); xiv size_t chunk_used = strlen ( chunk ); fifteen 16 if ( len - len_used < chunk_used ) { 17 len *= two ; xviii if (( line = realloc ( line , len )) == NULL ) { 19 perror ( "Unable to reallocate retentivity for the line buffer." ); twenty free ( line ); 21 exit ( 1 ); 22 } 23 } 24 25 // Copy the chunk to the terminate 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 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 gratis ( line ); 40 41 printf ( " \north\northward Max line size: %zd \north " , len ); 42 }
Delight notation, that in the in a higher place code, every fourth dimension the line buffer needs to be resized its capacity is doubled.
This is the result of running the above lawmaking on my motorcar. For brevity, I kept only the first lines of output:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 ii ~ $ ./t1 3 Lorem ipsum dolor sit 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. 8 |* 9 Aliquam erat volutpat. Mauris dignissim augue ac purus placerat scelerisque. Donec eleifend ut nibh eu elementum. 10 |*
You tin see that, this fourth dimension, we can print total lines of text and non fixed length chunks like in the initial arroyo.
Allow's modify the above code in order to print the line length instead of the actual text:
one // ... 2 three int master ( void ) { 4 // ... 5 half-dozen while ( fgets ( clamper , sizeof ( chunk ), fp ) != Nix ) { 7 8 // ... 9 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 ); xiii // "Empty" the line buffer 14 line [ 0 ] = '\0' ; 15 } 16 } 17 eighteen fclose ( fp ); nineteen free ( line ); 20 21 printf ( " \n\n Max line size: %zd \northward " , len ); 22 }
This is the result of running the modified code on my machine:
ane ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 ii ~ $ ./t1 iii line length: 57 4 line length: 136 5 line length: 147 6 line length: 114 vii line length: 112 8 line length: 95 9 line length: 62 10 line length: one 11 line length: 428 12 line length: ane thirteen line length: 460 14 line length: 1 15 line length: 834 16 line length: i 17 line length: 821 18 xix 20 Max line size: 1024
In the next instance, I will show you how to apply the getline office available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't take an equivalent function, so y'all won't be able to easily test this example on a Windows system. However, you should be able to exam it if you are using Cygwin or Windows Subsystem for Linux.
one #include <stdio.h> ii #include <stdlib.h> iii #include <string.h> 4 5 int primary ( void ) { 6 FILE * fp = fopen ( "lorem.txt" , "r" ); 7 if ( fp == NULL ) { eight perror ( "Unable to open up file!" ); nine exit ( ane ); 10 } 11 12 // Read lines using POSIX function getline 13 // This code won't work on Windows fourteen char * line = Goose egg ; fifteen size_t len = 0 ; 16 17 while ( getline ( & line , & len , fp ) != - 1 ) { eighteen printf ( "line length: %zd \n " , strlen ( line )); nineteen } xx 21 printf ( " \n\n Max line size: %zd \n " , len ); 22 23 fclose ( fp ); 24 costless ( line ); // getline volition resize the input buffer every bit necessary 25 // the user needs to free the memory when not needed! 26 }
Please note, how elementary is to use POSIX's 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 role.
When you lot use getline, don't forget to free the line buffer when you lot don't demand it anymore. Also, calling getline more than once will overwrite the line buffer, make a copy of the line content if you lot need to keep it for further processing.
This is the result of running the above getline example on a Linux motorcar:
1 ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2 2 ~ $ ./t2 three line length: 57 4 line length: 136 five line length: 147 6 line length: 114 7 line length: 112 8 line length: 95 nine line length: 62 10 line length: ane xi line length: 428 12 line length: ane 13 line length: 460 14 line length: 1 15 line length: 834 16 line length: i 17 line length: 821 18 xix 20 Max line size: 960
It is interesting to note, that for this particular case the getline part 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 unlike ways in which getline is implemented on different Unix like systems.
As mentioned earlier, getline is non present 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 virtually performant version of getline, but rather to implement a elementary replacement for non POSIX systems.
We are going to take the above instance and replace the POSIX'south getline version with our own implementation, say my_getline. Plainly, if you are on a POSIX system, you should utilise the version provided past the operating system, which was tested by countless users and tuned for optimal performance.
The POSIX getline part has this signature:
ane ssize_t getline ( char ** restrict lineptr , size_t * restrict north , FILE * restrict stream );
Since ssize_t is also a POSIX defined blazon, 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 nosotros are going to implement the function using the same approach as in one of the above examples, where I've divers a line buffer and kept copying chunks of text in the buffer until we found the end of line graphic symbol:
i // This will but have effect on Windows with MSVC two #ifdef _MSC_VER three #ascertain _CRT_SECURE_NO_WARNINGS 1 4 #define restrict __restrict 5
0 Response to "Read Every Line of a File C#"
Post a Comment