I would refuse if I didnโt add to the answers, probably one of the most standard ways of reading an unknown number of lines of unknown length from a text file. In C, you have two main methods for entering characters. (1) character-oriented input (i.e. getchar , getc , etc.); and (2) linearly oriented input (i.e. fgets , getline ).
From this combination of functions, the POSIX getline function by default will allocate sufficient space for reading a line of any length (up to the exhaustion of system memory). Also, when reading input lines , linearly oriented input is usually the right choice.
To read an unknown number of rows, a general approach is to allocate the expected number of pointers (in a-to-char array of pointers ), and then redistribute as needed if you finish more necessary. If you want to work with the complexities of overlaying pointers to a structure together in a linked list, this is fine, but itโs much easier to process an array of strings. (a linked list is more suitable if you have a structure with several members rather than a single line)
The process is simple. (1) allocates memory for some initial number of pointers ( LMAX below at 255 ), and then, when each line is read, (2) allocates memory to store the string and copy the string to an array (using strdup below which both (a) allocate memory to store strings and (b) copy the string to a new block of memory, returning a pointer to its address) (you assign the pointer returned to your string array[x] as array[x] )
As with any dynamic memory allocation, you are responsible for tracking the allocated memory by keeping a pointer to the beginning of each allocated memory block (so you can free it later) and then freeing the memory when it is no longer needed. (Use valgrind or some similar memory check to confirm that you have no memory errors and freed up all the created memory)
The following is an example of an approach that simply reads any text file and prints its lines before stdout before freeing up the memory allocated for storing the file. After you read all the lines (or while you read all the lines), you can easily parse your csv input into separate values.
Note: below, when LMAX lines were read, the array redistributed to hold twice as much as before, and reading continues. (You can set LMAX to 1 if you want to allocate a new pointer for each row, but this is a very inefficient way to handle memory allocation). Choosing some reasonable expected starting value and then redistributing the 2X current is the standard method of redistributing, but you can allocate additional blocks of any size that you choose.
Take a look at the code and let me know if you have any questions.
#include <stdio.h>
Using / Exit
$ ./bin/getline_rdfile dat/damages.txt Lines in file: array [ 0] Personal injury damage awards are unliquidated array [ 1] and are not capable of certain measurement; thus, the array [ 2] jury has broad discretion in assessing the amount of array [ 3] damages in a personal injury case. Yet, at the same array [ 4] time, a factual sufficiency review insures that the array [ 5] evidence supports the jury award; and, although array [ 6] difficult, the law requires appellate courts to conduct array [ 7] factual sufficiency reviews on damage awards in array [ 8] personal injury cases. Thus, while a jury has latitude in array [ 9] assessing intangible damages in personal injury cases, array [ 10] a jury damage award does not escape the scrutiny of array [ 11] appellate review. array [ 12] array [ 13] Because Texas law applies no physical manifestation array [ 14] rule to restrict wrongful death recoveries, a array [ 15] trial court in a death case is prudent when it chooses array [ 16] to submit the issues of mental anguish and loss of array [ 17] society and companionship. While there is a array [ 18] presumption of mental anguish for the wrongful death array [ 19] beneficiary, the Texas Supreme Court has not indicated array [ 20] that reviewing courts should presume that the mental array [ 21] anguish is sufficient to support a large award. Testimony array [ 22] that proves the beneficiary suffered severe mental array [ 23] anguish or severe grief should be a significant and array [ 24] sometimes determining factor in a factual sufficiency array [ 25] analysis of large non-pecuniary damage awards.
Memory check
$ valgrind ./bin/getline_rdfile dat/damages.txt ==14321== Memcheck, a memory error detector ==14321== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==14321== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==14321== Command: ./bin/getline_rdfile dat/damages.txt ==14321== Lines in file: array [ 0] Personal injury damage awards are unliquidated <snip> ... array [ 25] analysis of large non-pecuniary damage awards. ==14321== ==14321== HEAP SUMMARY: ==14321== in use at exit: 0 bytes in 0 blocks ==14321== total heap usage: 29 allocs, 29 frees, 3,997 bytes allocated ==14321== ==14321== All heap blocks were freed