0

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?

Xavier Dass
  • 575
  • 1
  • 9
  • 25
  • 3
    [strtok](http://linux.die.net/man/3/strtok) can help you parse the line based on given delimiters. There is no standard function to parse it all straight into an array as described. – kaylum Sep 11 '15 at 06:03
  • 1
    Take a look [HERE](http://stackoverflow.com/questions/3889992/how-does-strtok-split-the-string-into-tokens-in-c) and [HERE](http://www.cplusplus.com/reference/cstring/strtok/). – LPs Sep 11 '15 at 06:03
  • 1
    `if(strncmp(&line[i], ",",1) == 0)` --> `if( line[i] == ',')` – BLUEPIXY Sep 11 '15 at 06:20

0 Answers0