468,512 Members | 1,445 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

reading into an array.

hey guys,
i am trying to read a csv file into an 3d array.

the file format is the following:
ABC,"12 aug 2006",323.343,5454,4545.4545.4545...
..
...
...
...

for 762 rows and 7 columns.

how can i read this file into a 3D array, one for the column one for the rows and one for the length of the stream..

thanks guys.
Sep 24 '06 #1
2 4754
risby
30
hey guys,
i am trying to read a csv file into an 3d array.

the file format is the following:
ABC,"12 aug 2006",323.343,5454,4545.4545.4545...
..
...
...
...

for 762 rows and 7 columns.

how can i read this file into a 3D array, one for the column one for the rows and one for the length of the stream..

thanks guys.
You might get a better response if you gave some feedback to the previous attempts to help you, e.g. regarding separating fields in a csv file using fgets() and strtok()

http://www.thescripts.com/forum/post2098535-4.html

The code I posted here can be easily adapted by invoking strcpy() and possibly malloc() instead of simply fprintf() with each field read in. You'll also have to maintain column and row counters to index into your array.
Sep 24 '06 #2
you can use getline, but it's only useful if you have a fixed amount of elements on each line of the text file.
Expand|Select|Wrap|Line Numbers
  1.     while (!ifile.eof()) {
  2.         getline(ifile,first,',');
  3.         stdArray[j] = first;
  4.         getline(ifile,second,',');
  5.         stdArray[j] = second;
  6.         getline(ifile,third,' ' );
  7.         stdArray[j] = third;
  8.         getline(ifile,last);
  9.         stdArray[j] = last;
  10. }
  11.  
Make sure the file is open of course before you run the loop. The getline will read everything up to your delimiter, in this case it's "," and store it in your array. you can simply change it to a 3d array. Also, when using getline() to get the last element on the row of your cvs file, make sure you don't specify the delimiter, because it'll start reading the next line if you do.
Everything would have been much easier if you had space between elements. You could have used >> operator to read the file because it automatically scans for spaces.
Sep 24 '06 #3

Post your reply

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

Similar topics

2 posts views Thread by Dariusz | last post: by
6 posts views Thread by Dietrich Epp | last post: by
6 posts views Thread by Foxy Kav | last post: by
21 posts views Thread by JoKur | last post: by
10 posts views Thread by T Cordon | last post: by
21 posts views Thread by Stephen.Schoenberger | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.