RedFoxxie
RedFoxxie

Reputation: 45

File data split by ";" and then by "," in C

Basically I need to read the input from a file, the data on the file looks something like this:

A422TCRE234VD3KJ349D;2000,Gasoleo;Vermelho,;17,200,Platinum;17,200,45;
3KJ349DA422TCRE234VD;,diesel;Azul,Minivan;17,200,45;10,20,30;
DA422TC3KJ349RE234VD;,;Vermelho,;,,;,,;

Now what I need to do next is read this data and separate it by ";" which I'm doing using sscanf, like this:

sscanf(line," %[^;];%[^;];%[^;];%[^;];%[^;]", campos[0],campos[1], campos[2] ,campos[3],campos[4]);

"line" variable holds an entire line read from the file. All fine up until this point. Now I need to split each one of those arrays (campos[x]) by "," because I need to save each data on different structs I created. I was trying to achieve this by using an auxiliary array "output" where I combine all of the previous fields, like so:

strcpy(output,campos[1]);
strcat(output,",");
strcat(output,campos[2]);
strcat(output,",");
strcat(output,campos[3]);
strcat(output,",");
strcat(output,campos[4]);

Then I use sscanf again to try to split them:

sscanf(output, " %[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]",helper[0],helper[1],helper[2],helper[3],helper[4],helper[5],helper[6],helper[7],helper[8], helper[9]);

But though luck, it's not working, probably because as you can see on the input file, some of the atributes are empty, the thing is I need them to stay empty, because afterwards I need to show data to the user in the same format as the input file.

I've tried using strtok(), and many variations of the code I have here, nothing seems to work.

Just so you have a better idea I'll write here the output I'm currently having:

puts(output); -> 2000,Gasoleo,Vermelho,,17,200,Platinum,17,200,45
                ,diesel,Azul,Minivan,17,200,45,10,20,30
                ,,Vermelho,,,,,,,

printf("%s,%s\n", helper[0],helper[1]); -> 2000,Gasole
                                           ,
                                           ,

Also so I can give a better perspective, this file represents work orders for a car factory, so each line is as follows:

Word Order Id;Horsepower,Fuel Type;Colour,Model;Diameter,Width,Colour;Diameter,Width,Height;

Example: Horsepower and Fuel Type are engine attributes, each ";" separates between car parts.

What can I try to fix this?

Upvotes: 1

Views: 155

Answers (1)

anastaciu
anastaciu

Reputation: 23822

The fact that there are no spaces between the fields you want to parse makes it difficult to use sscanf or strtok for that matter.

Maybe a custom algorithm is the best way to go:

Live demo

int main() {

    FILE *f = fopen("file.txt", "r");

    if (f == NULL) {
        perror("File");
    }
    else {
        size_t i = 0, j = 0, it = 0;
        char line[100];
        char helper[12][100];
        while (fgets(line, sizeof(line), f)) {  
            int c;
            i = j = it = 0;
            while ((c = line[it]) != '\n' && c != '\0'){ //read and assign cycle
                if (c != ';' && c != ',') { //if character is not,  or ; assign
                    helper[i][j++] = c;
                }
                else {
                    helper[i][j] = '\0'; //else terminate string
                    i++;                 //go to next helper line
                    j = 0;               //reset column iterator
                }
                it++;
            }           
            for (size_t a = 1; a < i; a++){ //test print
                printf(",%s", helper[a]);
            }
            printf("\n");           
        }
    }
}

Output:

,2000,Gasoleo,Vermelho,,17,200,Platinum,17,200,45
,,diesel,Azul,Minivan,17,200,45,10,20,30
,,,Vermelho,,,,,,,

As requested.

Upvotes: 2

Related Questions