-3

I have an csv file and I want to get 2nd column's data. it is like that:

 1st row:    4   8   6
 2nd row:    3   2   2
 3rd row:    7   7   5

I will add them into AVLTREE but main problem is how to get specially 2nd column's data? They delimited with semicolon.

4;8;6;
3;2;2;
7;7;5;

So far I have just this code

int main()
{
int c;
char buffer[1000];
FILE *f = fopen("file.txt", "r");
if(f == NULL)
{
    printf("Error can't open file \n");
}
while (fgets(buffer,1000,f) != NULL)
{
    char *p = strtok(buffer, ";");
    while(p)
    {
        printf("%s\n", p);
        p=strtok(NULL, ";");
    }

    printf("%c",c);
}
fclose(f);
}
Greedy Pointer
  • 334
  • 5
  • 12

1 Answers1

2

If you want to parse CSV in C, I can advice you to use library. I'm using this one, and it work perfectly : https://sourceforge.net/projects/cccsvparser/

If moreover you want to do it by yourself, I advice you to check the strtok function : http://manpagesfr.free.fr/man/man3/strtok.3.html This function takes a string and delimiter in parameters and get you the next elements that match with the delimiter.

I can also advice you to check this link if you want to use strtok : How does strtok() split the string into tokens in C?

Sacha.R
  • 394
  • 2
  • 17