0

I have a file which looks like this:

text1, number, text2, text3,
text1, number, text2, text3,
...

I'd like to put all of these stuff into struct array:

struct myStruct {
    char string1[50];
    int dec;
    char string2[50];
    char string3[50];
}

I know that I can get line from a file using fgets() but I can't split it on smaller ones. I can't find any understandable solution to my problem so I write my own question. I tried with strtok() but I failed. Please write as simple as you can because I'm beginner with coding.

@Edit:

Here's my strtok() attempt:

token = strtok(line, " ");
if(token) {
    token[strlen(token)-1] = '\0';
    strcpy(tab[counter].name, token); /* String is stored how I want it (without a comma) */
}
token = strtok(NULL, " ");

token = strtok(line, " ");
if(token) {
    token[strlen(token)-1] = '\0'; /* And here it cuts last char from previous string */
    strcpy(year_tmp, token);
    year = atoi(year_tmp);
    tab[counter].year = year; /* And nothing is stored here because it isn't even what I want */
}
token = strtok(NULL, " ");
Dave
  • 11
  • 4

0 Answers0