Reading data from a csv file...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • timkenx
    New Member
    • Dec 2006
    • 4

    Reading data from a csv file...

    i have a csv file with lots of data. I'm trying to create a 2d array to store this data.

    However I have no idea how to do this. This is how far i've got so far...

    char a[135][21];
    char b[10];
    int i=0;
    int j=0;
    for (j=0;j<22;j++)
    {
    for (i=0;i<136;i++)
    {
    a[j][i]=fin.getline(b, 20,',');
    }
    }

    Basically, the data in the csv file starts a new 'row' after the 136th value.

    The problem is, I can't take data out of the file and then store it in the array, it won't let me! Can anyone help. Also, the csv file contains letters and numbers.
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by timkenx
    i have a csv file with lots of data. I'm trying to create a 2d array to store this data.

    However I have no idea how to do this. This is how far i've got so far...

    char a[135][21];
    char b[10];
    int i=0;
    int j=0;
    for (j=0;j<22;j++)
    {
    for (i=0;i<136;i++)
    {
    a[j][i]=fin.getline(b, 20,',');
    }
    }

    Basically, the data in the csv file starts a new 'row' after the 136th value.

    The problem is, I can't take data out of the file and then store it in the array, it won't let me! Can anyone help. Also, the csv file contains letters and numbers.
    Hi. Brave attempt and there is no way that you can put different types into the same array.

    You need to know what data type is at what point in the csv file. Create a struct for each line;

    Code:
    struct records {
       char cola;
       char colb;
       long colc;
       float cold;
    }
    then create an array of this struct;
    Code:
    records *myrecords;
    myrecords = new records[lines_in_file];
    and load the csv file into this array
    good luck

    Comment

    Working...