Hello,

I am looking some help with a program. I am trying to parse the following file into a dynamic Array "Database[8][5]".

Device ,Gain (beta),Power Pd(W) ,Current Ic(A) ,BVceo(V)
2N3904 ,150,0.35,0.2,40
2N2202 ,120,0.5,0.3,35
2N3055 ,60,120,10,90
2N1013 ,95,50,4,110
MPE106 ,140,15,1.5,35
MC1301 ,80,10,0.9,200
ECG1201 ,130,1.3,1.1,55

I seem to beable to parse the data but it is not loading the data into the array.
Any help would be appricated. My code so far is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* Database[8][5];

char temp;

int main()
{
    FILE *file;
    file = fopen("Task4Table.csv", "r");

    if(file == NULL)
    {
        printf("Could not open the database");
        exit(0);
    }

    char buf[1000];
    int i = 0;
    int j = 0;
    while(fgets(buf,1000,file))
    {

        j = 0;
        i++;

        char* field = strtok(buf, ",");
        while (field != NULL)
        {
            if (j==0)
            {
                Database[i][j] = field;
            }
            if (j==1)
            {
               Database[i][j] = field;
            }
            if (j==2)
            {
               Database[i][j] = field;
            }
            if (j==3)
            {
               Database[i][j] = field;
            }

            printf("%s \t", field);

            field = strtok(NULL, ",");

            j++;

        }

    }

    fclose(file);

    printf("array value is: %s", Database[1][1]);

return 0;

}

Think about line 5. You declared an array of pointers. Then you parse your content and point at your field but then overwrite what is stored at field.

Since the only storage is field all pointers point at field. You'll have to rethink where the strings are stored at and where your pointers point to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.