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

how to use arrays

P: 25
how can i read something into an 3D array??
Sep 28 '06 #1
Share this Question
Share on Google+
7 Replies


Banfa
Expert Mod 5K+
P: 8,916
That question is really too generic to answer, there is not enough context.

Expand|Select|Wrap|Line Numbers
  1. int array[10][10][10];
  2.  
  3. array[3][2][7] = 5;
  4.  
Sep 28 '06 #2

P: 25
i mean,
i have to read data from a file into the array,
the data in the file i am reading are seperated by comma,
i want to seperate the data from the file into the the array so it can be accessed easily later.
Sep 29 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
In that case rather than a 3 dimensional array I would suggest an array of structures with each structure corresponding to a line of data from the file.

If you don't want to use structures then you still only need a 2D array, a file only has 2 dimensions (columns and lines (usless of corse you plan to track it's changes over time in which case you have the 3rd dimension of time (if you wish to classify time as a dimension I believe the jury is still out on that)))

Don't try to tackle the problem in 1 chunk.

Step 1 open the file and read the lines

Step 2 parser the lines into you data structures

Step 3 perform some operation on the data.
Sep 29 '06 #4

P: 25
sorry for this but how do i parser????
Sep 29 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
That should read parse and it depends entirely on what data is on the line and how you wish to store it.

A combination of copying for string data and using strtol, strtoul and strtof for integer and floating point data should allow you to get the data from your line into your structure.
Sep 29 '06 #6

P: 25
sorry but still dont understand!!!!
can you explain it more
Oct 1 '06 #7

Banfa
Expert Mod 5K+
P: 8,916
If the data you want is text then you set-up a pointer to the start of the text in you line read from the file and copy the required number of characters (bytes) from that pointer to the destination pointer or string.

If the data you want to read is an integer then you set up a pointer to the start of the data in the line of data read from the file and make a call using that pointer to one of the functions strtoul or strtol

If the data you want to read is a floating point number then you set up a pointer to the start of the data in the line of data read from the file and make a call using that pointer to one of the function strtof

Basically you use this pseudo code

Expand|Select|Wrap|Line Numbers
  1. Read Data From File Into Variable LINEDATA
  2. Pointer Pointer Variable P At LINEDATA
  3. While P is not at the end of LINEDATA
  4.     If Data At P Is required
  5.         Copy Data At P Into Data Structure
  6.         Move P On By The Number Of Converted Bytes of Data
  7.     Else
  8.         Move P on 1 Byte
  9.     EndIf
  10. EndWhile
  11.  
Oct 1 '06 #8

Post your reply

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