I've looked around and couldn't find a simple solution to this question. Basically, what I have is a text file with 9 columns of information, each column ends with a comma ",".
(It looks something like below, but with many more rows)
0,2,100,100,"foobar", "", 986389763, 598752398, 4
1, ,235,224,"fizz buzz", "bizzfuzz", 884389763, 294772298, 1
I am trying to get the 7th column of information. I was hoping to find a function that would store a line as an array, separating each element by a comma. So to access the column I'd need would be as simple as accessing line[6]
At the moment, I'm doing it in a really round-a-bout way using fgets()
.
while( fgets(line, sizeof line, input_file) !=NULL) {
int commaCount = 0;
for(int i = 0; i < sizeof line; i++) {
if(commaCount == 6) {
//store next x amount of characters in a new array
}
if(strncmp(&line[i], ",",1) == 0) {
commaCount++;
}
}
I didn't give you all of the code, but I think this is enough to see how much of a headache doing it like this is.
In summary, I am wondering if there is a more efficient way to do what I am trying to do here?