C: Reading a file from a txt file and pasting into arrays. comma as a separator

So, I have a .txt file that has entries similar to:

1234567, John, Doe

and I have arrays in my C code where I want to read these values ​​and paste them into:

int id[36] = {0};
char first_name[36];
char last_name[36];

So the idea is that, for example, 1234567 is at index 0 id, John is at index 0 first_name, and doe is at index 0 last_name. And I want to do this with 36 similar lines.

I researched FILE IO, but I did not find anything in this regard. What is the best way to do this? Thanks for any answers.

+4
source share
2 answers

, fscanf. 3, , :

// id is OK as os
int id[36] = {0};
// make first_name and last_name arrays of arrays
char first_name[36][36];
char last_name[36][36];
int i = 0;
while (fscanf(fd, "%d, %35[^,], %35s", &id[i], first_name[i], last_name[i]) == 3) {
    i++;
    if (i == 36) {
        break;
    }
}

fscanf , int, , 35 , 35 .

+6

fscanf strtok,

+1

Source: https://habr.com/ru/post/1584805/


All Articles