how can i read something into an 3D array??
how to use arrays
Collapse
X
-
Tags: None
-
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.Comment
-
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.Comment
-
-
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.Comment
-
-
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
Code:Read Data From File Into Variable LINEDATA Pointer Pointer Variable P At LINEDATA While P is not at the end of LINEDATA If Data At P Is required Copy Data At P Into Data Structure Move P On By The Number Of Converted Bytes of Data Else Move P on 1 Byte EndIf EndWhile
Comment
Comment