Vb Net Read Text File Line by Line Into Array
Solarian Developer
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 show yous how to read a text file line by line in C using the standard C role fgets and the POSIX getline function. At the terminate of the article, I volition 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 trivial trouble in many programming languages, but not in C. The standard style 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 can find all the code examples and the input file at the GitHub repo for this article.
Permit's start with a simple example of using fgets to read chunks from a text file. :
1 #include <stdio.h> two #include <stdlib.h> iii 4 int chief ( void ) { 5 FILE * fp = fopen ( "lorem.txt" , "r" ); half dozen if ( fp == Nada ) { 7 perror ( "Unable to open file!" ); eight exit ( i ); nine } 10 11 char chunk [ 128 ]; 12 13 while ( fgets ( chunk , sizeof ( clamper ), fp ) != NULL ) { 14 fputs ( clamper , stdout ); xv fputs ( "|* \n " , stdout ); // marker cord used to evidence where the content of the chunk assortment has concluded sixteen } 17 18 fclose ( fp ); xix }
For testing the code I've used a simple dummy file, lorem.txt. This is a slice from the output of the above plan on my machine:
one ~ $ clang -std=c17 -Wall -Wextra -pedantic t0.c -o t0 two ~ $ ./t0 iii Lorem ipsum dolor sit down amet, consectetur adipiscing elit. 4 |* v 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 marking string.
If you watch carefully, by scrolling the above 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 tin can store an unabridged line from the original text file just if the line can fit within our chunk array.
What if you need to have the unabridged line of text bachelor for further processing and not a slice of line ? A possible solution is to copy or concatenate chunks of text in a separate line buffer until we find the end of line grapheme.
Let'due south start by creating a line buffer that will store the chunks of text, initially this volition have the same length as the clamper array:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> four v int main ( void ) { 6 FILE * fp = fopen ( "lorem.txt" , "r" ); seven // ... 8 ix char chunk [ 128 ]; 10 11 // Shop the chunks of text into a line buffer 12 size_t len = sizeof ( chunk ); 13 char * line = malloc ( len ); fourteen if ( line == NULL ) { 15 perror ( "Unable to allocate memory for the line buffer." ); 16 exit ( 1 ); 17 } eighteen xix // "Empty" the string 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 string, until nosotros find the end of line character. If necessary, we'll resize the line buffer:
ane #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 v int master ( void ) { 6 // ... vii eight // "Empty" the string 9 line [ 0 ] = '\0' ; 10 11 while ( fgets ( chunk , sizeof ( clamper ), fp ) != NULL ) { 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 ; 18 if (( line = realloc ( line , len )) == NULL ) { nineteen perror ( "Unable to reallocate memory for the line buffer." ); xx free ( line ); 21 exit ( 1 ); 22 } 23 } 24 25 // Copy the chunk to the finish of the line buffer 26 strncpy ( line + len_used , chunk , len - len_used ); 27 len_used += chunk_used ; 28 29 // Bank check if line contains '\northward', if yes procedure the line of text thirty if ( line [ len_used - 1 ] == '\n' ) { 31 fputs ( line , stdout ); 32 fputs ( "|* \due north " , stdout ); 33 // "Empty" the line buffer 34 line [ 0 ] = '\0' ; 35 } 36 } 37 38 fclose ( fp ); 39 gratis ( line ); xl 41 printf ( " \n\north Max line size: %zd \n " , len ); 42 }
Please note, 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 upshot of running the in a higher place lawmaking on my machine. For brevity, I kept only the showtime lines of output:
ane ~ $ 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. 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 air conditioning purus placerat scelerisque. Donec eleifend ut nibh eu elementum. 10 |*
Yous can see that, this time, we can print full lines of text and not fixed length chunks like in the initial arroyo.
Let'south modify the above code in order to print the line length instead of the actual text:
i // ... 2 3 int main ( void ) { four // ... 5 6 while ( fgets ( chunk , sizeof ( clamper ), fp ) != NULL ) { 7 8 // ... 9 ten // Check if line contains '\n', if yes process the line of text 11 if ( line [ len_used - i ] == '\n' ) { 12 printf ( "line length: %zd \due north " , len_used ); 13 // "Empty" the line buffer 14 line [ 0 ] = '\0' ; 15 } 16 } 17 xviii fclose ( fp ); 19 costless ( line ); 20 21 printf ( " \due north\due north Max line size: %zd \northward " , len ); 22 }
This is the result of running the modified lawmaking on my machine:
i ~ $ clang -std=c17 -Wall -Wextra -pedantic t1.c -o t1 2 ~ $ ./t1 3 line length: 57 four line length: 136 5 line length: 147 half dozen line length: 114 7 line length: 112 8 line length: 95 ix line length: 62 10 line length: 1 11 line length: 428 12 line length: 1 xiii line length: 460 14 line length: i 15 line length: 834 sixteen line length: 1 17 line length: 821 18 nineteen twenty Max line size: 1024
In the next example, I will show you lot how to use the getline function bachelor on POSIX systems like Linux, Unix and macOS. Microsoft Visual Studio doesn't have an equivalent function, then you won't be able to easily examination this example on a Windows system. However, yous should exist able to exam it if you are using Cygwin or Windows Subsystem for Linux.
1 #include <stdio.h> ii #include <stdlib.h> 3 #include <cord.h> 4 five int main ( void ) { half dozen FILE * fp = fopen ( "lorem.txt" , "r" ); vii if ( fp == Zero ) { 8 perror ( "Unable to open file!" ); 9 get out ( 1 ); ten } xi 12 // Read lines using POSIX part getline thirteen // This code won't work on Windows 14 char * line = Null ; 15 size_t len = 0 ; 16 17 while ( getline ( & line , & len , fp ) != - 1 ) { eighteen printf ( "line length: %zd \n " , strlen ( line )); xix } 20 21 printf ( " \n\n Max line size: %zd \due north " , len ); 22 23 fclose ( fp ); 24 gratis ( line ); // getline will resize the input buffer every bit necessary 25 // the user needs to free the memory when not needed! 26 }
Please note, how simple is to use POSIX'due 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 y'all apply getline, don't forget to free the line buffer when you don't need it anymore. Also, calling getline more once will overwrite the line buffer, make a copy of the line content if y'all demand to keep information technology for further processing.
This is the result of running the above getline example on a Linux machine:
ane ~ $ 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 7 line length: 112 8 line length: 95 9 line length: 62 10 line length: ane 11 line length: 428 12 line length: i 13 line length: 460 14 line length: 1 fifteen line length: 834 xvi line length: ane 17 line length: 821 18 xix 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 run the same lawmaking on macOS the line buffer is resized to 1024 bytes. This is due to the dissimilar means in which getline is implemented on different Unix like systems.
As mentioned before, getline is not present in the C standard library. It could be an interesting exercise to implement a portable version of this role. The idea here is non to implement the most performant version of getline, only rather to implement a unproblematic replacement for non POSIX systems.
We are going to accept the above instance and replace the POSIX's getline version with our own implementation, say my_getline. Obviously, if you lot are on a POSIX organization, you should use the version provided by the operating organization, 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 also a POSIX defined type, usually a 64 bits signed integer, this is how we are going to declare our version:
one 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 aforementioned arroyo as in one of the above examples, where I've defined a line buffer and kept copying chunks of text in the buffer until nosotros found the finish of line character:
one // This will just have upshot on Windows with MSVC two #ifdef _MSC_VER 3 #define _CRT_SECURE_NO_WARNINGS 1 4 #define restrict __restrict 5