By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,911 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,911 IT Pros & Developers. It's quick & easy.

Reading data from a csv file...

P: 4
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.
Dec 18 '06 #1
Share this Question
Share on Google+
1 Reply


100+
P: 1,646
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;

Expand|Select|Wrap|Line Numbers
  1. struct records {
  2.    char cola;
  3.    char colb;
  4.    long colc;
  5.    float cold;
  6. }
then create an array of this struct;
Expand|Select|Wrap|Line Numbers
  1. records *myrecords;
  2. myrecords = new records[lines_in_file];
and load the csv file into this array
good luck
Dec 18 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.