Read File Line by Line in C Using Fgetc
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 past Paul
In this article, I will evidence you lot how to read a text file line by line in C using the standard C function fgets and the POSIX getline function. At the end of the commodity, I volition write a portable implementation of the getline part that can exist used with whatever standard C compiler.
Reading a file line by line is a footling problem in many programming languages, simply non in C. The standard mode of reading a line of text in C is to use the fgets part, which is fine if you know in advance how long a line of text could be.
You tin find all the code examples and the input file at the GitHub repo for this article.
Permit'due south first with a elementary example of using fgets to read chunks from a text file. :
1 #include <stdio.h> 2 #include <stdlib.h> 3 four int main ( void ) { 5 FILE * fp = fopen ( "lorem.txt" , "r" ); vi if ( fp == Zilch ) { vii perror ( "Unable to open up file!" ); eight go out ( 1 ); nine } 10 xi char chunk [ 128 ]; 12 13 while ( fgets ( clamper , sizeof ( clamper ), fp ) != NULL ) { 14 fputs ( chunk , stdout ); 15 fputs ( "|* \northward " , stdout ); // marker cord used to bear witness where the content of the clamper array has concluded xvi } 17 eighteen fclose ( fp ); xix } 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 motorcar:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0 ii ~ $ ./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 |* ix dignissim molestie. 10 |* The code prints the content of the chunk array, as filled after every telephone call to fgets, and a marker cord.
If you lot watch carefully, by scrolling the higher up text snippet to the correct, you can see that the output was truncated to 127 characters per line of text. This was expected considering our code tin can store an entire line from the original text file merely if the line can fit within our chunk assortment.
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 separate line buffer until we observe the finish of line graphic symbol.
Permit's offset by creating a line buffer that will store the chunks of text, initially this will have the aforementioned length as the chunk assortment:
1 #include <stdio.h> 2 #include <stdlib.h> three #include <string.h> 4 5 int master ( void ) { 6 FILE * fp = fopen ( "lorem.txt" , "r" ); vii // ... eight 9 char chunk [ 128 ]; 10 11 // Store the chunks of text into a line buffer 12 size_t len = sizeof ( chunk ); thirteen char * line = malloc ( len ); 14 if ( line == Zippo ) { 15 perror ( "Unable to allocate memory for the line buffer." ); 16 get out ( 1 ); 17 } 18 xix // "Empty" the string 20 line [ 0 ] = '\0' ; 21 22 // ... 23 24 } Side by side, we are going to append the content of the chunk array to the end of the line cord, until we discover the cease of line character. If necessary, we'll resize the line buffer:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int chief ( void ) { half dozen // ... 7 8 // "Empty" the string nine line [ 0 ] = '\0' ; 10 11 while ( fgets ( clamper , sizeof ( clamper ), fp ) != Zero ) { 12 // Resize the line buffer if necessary thirteen size_t len_used = strlen ( line ); 14 size_t chunk_used = strlen ( chunk ); 15 16 if ( len - len_used < chunk_used ) { 17 len *= ii ; 18 if (( line = realloc ( line , len )) == Zilch ) { nineteen perror ( "Unable to reallocate memory for the line buffer." ); xx complimentary ( line ); 21 get out ( 1 ); 22 } 23 } 24 25 // Copy the chunk to the terminate of the line buffer 26 strncpy ( line + len_used , chunk , len - len_used ); 27 len_used += chunk_used ; 28 29 // Check if line contains '\northward', if yes procedure the line of text 30 if ( line [ len_used - 1 ] == '\north' ) { 31 fputs ( line , stdout ); 32 fputs ( "|* \north " , stdout ); 33 // "Empty" the line buffer 34 line [ 0 ] = '\0' ; 35 } 36 } 37 38 fclose ( fp ); 39 free ( line ); xl 41 printf ( " \n\due north Max line size: %zd \n " , len ); 42 } Please note, that in the above code, every time the line buffer needs to be resized its capacity is doubled.
This is the outcome of running the above 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 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 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 air conditioning purus placerat scelerisque. Donec eleifend ut nibh european union elementum. ten |* Yous can see that, this time, we can impress full lines of text and not fixed length chunks like in the initial approach.
Permit's modify the above code in gild to print the line length instead of the actual text:
1 // ... two three int primary ( void ) { 4 // ... 5 half dozen while ( fgets ( chunk , sizeof ( chunk ), fp ) != Zip ) { seven 8 // ... ix 10 // Check if line contains '\n', if yes process the line of text 11 if ( line [ len_used - 1 ] == '\n' ) { 12 printf ( "line length: %zd \due north " , len_used ); 13 // "Empty" the line buffer fourteen line [ 0 ] = '\0' ; 15 } 16 } 17 18 fclose ( fp ); 19 free ( line ); xx 21 printf ( " \due north\n Max line size: %zd \n " , len ); 22 } This is the result of running the modified lawmaking on my machine:
1 ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 ii ~ $ ./t1 three line length: 57 4 line length: 136 v line length: 147 vi line length: 114 seven line length: 112 8 line length: 95 ix line length: 62 ten line length: 1 11 line length: 428 12 line length: ane 13 line length: 460 14 line length: i 15 line length: 834 16 line length: i 17 line length: 821 18 19 20 Max line size: 1024 In the next example, I will show yous how to use the getline part available on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, so you won't be able to easily test this case on a Windows system. However, y'all should exist able to exam it if you are using Cygwin or Windows Subsystem for Linux.
i #include <stdio.h> 2 #include <stdlib.h> 3 #include <cord.h> 4 5 int main ( void ) { 6 FILE * fp = fopen ( "lorem.txt" , "r" ); 7 if ( fp == NULL ) { 8 perror ( "Unable to open file!" ); 9 exit ( 1 ); 10 } xi 12 // Read lines using POSIX office getline 13 // This lawmaking won't work on Windows 14 char * line = NULL ; fifteen size_t len = 0 ; xvi 17 while ( getline ( & line , & len , fp ) != - 1 ) { 18 printf ( "line length: %zd \due north " , strlen ( line )); xix } 20 21 printf ( " \n\n Max line size: %zd \due north " , 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 not needed! 26 } Please note, how simple is to use POSIX's 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 function.
When you apply getline, don't forget to gratuitous the line buffer when you don't need it anymore. Also, calling getline more than once will overwrite the line buffer, make a copy of the line content if you demand to keep it for further processing.
This is the event of running the above getline example on a Linux machine:
one ~ $ clang -std=gnu17 -Wall -Wextra -pedantic t2.c -o t2 2 ~ $ ./t2 3 line length: 57 4 line length: 136 five line length: 147 6 line length: 114 seven line length: 112 8 line length: 95 9 line length: 62 10 line length: 1 11 line length: 428 12 line length: 1 thirteen line length: 460 14 line length: 1 15 line length: 834 16 line length: i 17 line length: 821 18 19 20 Max line size: 960 It is interesting to note, that for this particular case the getline function on Linux resizes the line buffer to a max of 960 bytes. If you lot run the same 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 similar systems.
As mentioned before, getline is not present in the C standard library. Information technology could exist an interesting exercise to implement a portable version of this function. The idea here is non to implement the nearly performant version of getline, simply rather to implement a uncomplicated replacement for non POSIX systems.
We are going to take the above example and supersede the POSIX'south 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 performance.
The POSIX getline function has this signature:
1 ssize_t getline ( char ** restrict lineptr , size_t * restrict n , FILE * restrict stream ); Since ssize_t is as well 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 nosotros are going to implement the function using the same arroyo as in one of the in a higher place examples, where I've divers a line buffer and kept copying chunks of text in the buffer until nosotros found the end of line character:
1 // This will simply have effect on Windows with MSVC 2 #ifdef _MSC_VER 3 #define _CRT_SECURE_NO_WARNINGS 1 4 #ascertain restrict __restrict five
0 Response to "Read File Line by Line in C Using Fgetc"
Post a Comment