Question: store .dat file into array.. really confused

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 3998 Views - Last Post: 26 April 2011 - 03:34 PM Rate Topic: -----

#1 TheScandal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-April 11

Question: store .dat file into array.. really confused

Posted 25 April 2011 - 02:24 PM

So I've looked all over the interweb, and I can't find anything that make sense to me and answers my question. Remember, I am a beginner, so all the fancy shortcuts wont help me.

So I got a homework problem where I must use a .dat file and store the data into arrays (there's more to the homework, but this is just part of it). The first line of the data file contains the number of 3-dimensional points that define the model. We call these points the vertices of the model. For this model, the number is exactly 522. So this is an example of the data file,

522
-10900 -200 -19600
-10900 -100 -17800
-10900 -100 -20800

The next line of data contains the number of pen movements to draw the model. For this model, the number is exactly 891. Here is a little bit of that.

10900 400 -18700 (*this is from the 522*)
891 (*start of the new data*)
14 0
15 15

Note: The second number is a color to use when drawing a line to that vertex

I just really need an explanation of what to do and an example. I know that I must use the ifstream. But once I open the file, I have no idea how to store that data in the arrays.

//Constants
const size_t M = 522;
const size_t N = 891;
const int S = 500;


int main()
{
    double data[M][3]; //this is the 522 data
    unsigned int vertex[N]; //
    int color[N];
    size_t i, j;

    ifstream shuttle;
    shuttle.open("shuttle.dat");
    if(shuttle.fail())
    {
	cout << "Input file opening failed. \n";
	exit(1);
    }

    //A function for the homework goes here, but I haven't written that because I want to figure out how to store the data first

    shuttle.close();
        
    return END_SUCCESS;
}



I believe that I must get the thing to read the first line and figure out how many lines of numbers it will use. Then store that into an array. Then once it reads 891, it will store the next 891 lines into another array. But I don't know how to put my words into code.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Question: store .dat file into array.. really confused

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,569
  • Joined: 25-December 09

Re: Question: store .dat file into array.. really confused

Posted 25 April 2011 - 03:25 PM

My first question is why are you using an array? This problem is really shouting for the use of std::vector.

Now since your first line is 522 does that mean that there will be 522 lines of 3 data points or 522 total data points? If it is 522 total data points then your array data[M][3] is larger than you need 522 / 3 = 174. Since you have 3 entries per line you could use a couple of loops to retrieve the data. The outer loop to based on the number of lines the inner loop based on the number of data items per line. You may even want to put this into a function so you can re-use the code for each section of your file reading.

After you retrieve the first vector/array then you should be at your next starting point. Do the same as above but based on the new number of data points (891) divided by the number of items per line.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 TheScandal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-April 11

Re: Question: store .dat file into array.. really confused

Posted 25 April 2011 - 04:37 PM

View Postjimblumberg, on 25 April 2011 - 03:25 PM, said:

My first question is why are you using an array? This problem is really shouting for the use of std::vector.

Now since your first line is 522 does that mean that there will be 522 lines of 3 data points or 522 total data points? If it is 522 total data points then your array data[M][3] is larger than you need 522 / 3 = 174. Since you have 3 entries per line you could use a couple of loops to retrieve the data. The outer loop to based on the number of lines the inner loop based on the number of data items per line. You may even want to put this into a function so you can re-use the code for each section of your file reading.

After you retrieve the first vector/array then you should be at your next starting point. Do the same as above but based on the new number of data points (891) divided by the number of items per line.

Jim


sorry, my mind is a bit scattered. Early finals are rough...

Ok so I attached the data file... (it wont let be upload it so here a link)
http://www.portmain..../hw/shuttle.dat


Now, i just started on this hw, because of my early final. Now it says that I must read the data file into a data structure. But we just learned about those last friday. So I don't got a clue how they work.

the 522 at the beginning of the long list is the number of vertices. So let's take data[0][0], data[0][1], and data[0][2]. There should be equal to -10900, -200, and -19600. So all 3 is a point in a world coordinate system. So it would be x, y, and z respectively.

So there are 522 points in space that are vertices.

The next bit of data 891 is the number of lines that connect to each point from the 522 data.

So pretty much, I'm making a wire frame of a space shuttle....

for(i = 0; i < M; ++i)
    {
	for(j = 0; j < 3; ++j)
	{
	    shuttle >> data[i][j];
	}
    }



I tried this and it works, but it doesn't do what I want it to do. Because it reads data[0][0] as 522. I dont want that. But I don't know how to prevent it from doing that.

This post has been edited by TheScandal: 25 April 2011 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,569
  • Joined: 25-December 09

Re: Question: store .dat file into array.. really confused

Posted 25 April 2011 - 04:40 PM

Since the 522 appears to be the first entry just read it into a different variable, before you start reading the next line.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 TheScandal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-April 11

Re: Question: store .dat file into array.. really confused

Posted 25 April 2011 - 05:17 PM

View Postjimblumberg, on 25 April 2011 - 04:40 PM, said:

Since the 522 appears to be the first entry just read it into a different variable, before you start reading the next line.

Jim


Well we have to do an "assert" to make sure that there are 522 vertices. So storing it in a variable is probably what I have to do. but how do I do that, and have the array ignore that one?

Then how do i make it so that the 891 data is placed in an array too?

This is what I want, in english..

while(once shuttle reads 522 and shuttle does not read 891)
{
for(i = 0; i < M; ++i)
{
for(j = 0; j < 3; ++j)
{
shuttle >> data[i][j];
}
}
if(shuttle reads 891)
{
for(i = 0; i < N; ++i)
{
for(j = 0; j < 2; ++j)
{
shuttle >> vertex[i];
shuttle >> color[j];
}
}
}
}



once the file or whatever reads 522, it will begin on the next line and store the data. Once it reads 891, it stores the data as well (in 2 different arrays)

So how do I get it to read 522, and not store it in an array but in a variable, and get it to go to the next line and begin storing the data?


P.S.
This is what I have to make
http://www.portmain..../hw/shuttle.jpg

This post has been edited by TheScandal: 25 April 2011 - 05:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,569
  • Joined: 25-December 09

Re: Question: store .dat file into array.. really confused

Posted 25 April 2011 - 05:34 PM

Is the number 522 the on the first line of the data file?

Is there 522 * 3 data points for the first array or 522 total data points?

Maybe you could post the data file?

Jim

This post has been edited by jimblumberg: 25 April 2011 - 05:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 TheScandal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-April 11

Re: Question: store .dat file into array.. really confused

Posted 25 April 2011 - 05:48 PM

View Postjimblumberg, on 25 April 2011 - 05:34 PM, said:

Is the number 522 the on the first line of the data file?

Is there 522 * 3 data points for the first array or 522 total data points?

Maybe you could post the data file?

Jim


I have posted a link to the data file, but here it is again
http://www.portmain..../hw/shuttle.dat

yes, 522 is in the first line. And 891 is on line 524 (assuming 522 is on line 1 of the data file, so
-10900 -200 -19600 is on line 2)

So if we made a matrix. It would be a 522 x 3 matrix..... ok, I'm getting confused at what you are saying. The line after 522, -10900 -200 -19600 (tells us the first point in space (x, y, z)) would be the first point.

Now -10900 is the first data point, if that is what you are talking about?
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,519
  • Joined: 23-August 08

Re: Question: store .dat file into array.. really confused

Posted 25 April 2011 - 05:53 PM

Don't make things multidimensional where they do not need to be.

I see a data structure:
-10900 -200 -19600


and another data structure:
14 0 

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,569
  • Joined: 25-December 09

Re: Question: store .dat file into array.. really confused

Posted 25 April 2011 - 06:08 PM

The first line contains the number of vertices. (522) Read and store into a variable. You could name your variable something like numVertices.

The second line contains matrix[0][0] = -10900 , matrix[0][1] = -200, and matrix[0][2] = -19600.
Line 3 contains matrix[1][0] = -10900, matrix[1][1] = -100, and matrix[1][2] = -17800.
and line so on until line 523 which contains: matrix[521][0] = 10900, matrix[521][1] = 400, and matrix[521][2] = -18700. For a total of 522 lines of 3 data items.

Then the next item after you read matrix[521][2] will be the number of lines (891).
Now you have 891 lines of 2 data points.
So after you read the 891 you will read line[0][0] = 14, line[0][1] = 0.
and repeat until you read 891 lines.
Then the next line will be "Shuttle, origin unknown. Rebuilt from a Purdue student project and"

And as JackOfAllTrades suggested this really would be easier with a couple of structures.

Jim

This post has been edited by jimblumberg: 25 April 2011 - 06:11 PM

Was This Post Helpful? 1
  • +
  • -

#10 TheScandal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-April 11

Re: Question: store .dat file into array.. really confused

Posted 25 April 2011 - 07:43 PM

View Postjimblumberg, on 25 April 2011 - 06:08 PM, said:

The first line contains the number of vertices. (522) Read and store into a variable. You could name your variable something like numVertices.

The second line contains matrix[0][0] = -10900 , matrix[0][1] = -200, and matrix[0][2] = -19600.
Line 3 contains matrix[1][0] = -10900, matrix[1][1] = -100, and matrix[1][2] = -17800.
and line so on until line 523 which contains: matrix[521][0] = 10900, matrix[521][1] = 400, and matrix[521][2] = -18700. For a total of 522 lines of 3 data items.

Then the next item after you read matrix[521][2] will be the number of lines (891).
Now you have 891 lines of 2 data points.
So after you read the 891 you will read line[0][0] = 14, line[0][1] = 0.
and repeat until you read 891 lines.
Then the next line will be "Shuttle, origin unknown. Rebuilt from a Purdue student project and"

And as JackOfAllTrades suggested this really would be easier with a couple of structures.

Jim


yep, there we go. Now we got it.

Yea, a structure would be easier, for you. But we were just taught this friday and this is the last week of classes before finals. And so structures are over my head (the text book just sums it up for me and gives me nothing meaningful).

Now, what you said is what I've been trying to explain to you. Now back to my original question.. how do I do this? I know what I need to do, just not how. Like am I using a for-loop in the structure (if I use one)? What is the syntax in order to make an array store the data file?

It's the syntax, the computer language, that I'm having a hard time understanding. I don't know how to convey my wishes to the computer through its language, especially since I haven't been taught nor learned the syntax for this.

I'm more of a visual learner here. Like telling me is great and all, but it's tough for me to understand, unless I see it in action, and mess with on my computer.

Let's say I got a .dat file (let's call it test.dat).

test.dat has the following

3
1 2 3
4 5 6
7 8 9
2
7 8
9 10

It's the same thing, but cut down.

So my question is, how do I store these data points in a 2d array? AKA, a 3x3 matrix and a 2x2 matrix. And store the 2 and the 3 as variables for the vertices or whatever.


Thanks
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,569
  • Joined: 25-December 09

Re: Question: store .dat file into array.. really confused

Posted 25 April 2011 - 07:51 PM

First I suggest that you start even simpler:

Take a data file that contains these entries:

1 2 3
4 5 6
7 8 9

Read this file to place these values into an single array mArray[?] of the proper size.

Give it a try and post code where you try to read this file. You say you are a visual learner so you must write the code to see what is happening. Start with a simple main function, open your data file, read the data file one number at a time into an array. Then print out the array to insure you read the variables correctly.



Jim

This post has been edited by jimblumberg: 25 April 2011 - 07:52 PM

Was This Post Helpful? 1
  • +
  • -

#12 TheScandal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-April 11

Re: Question: store .dat file into array.. really confused

Posted 26 April 2011 - 01:27 PM

View Postjimblumberg, on 25 April 2011 - 07:51 PM, said:

First I suggest that you start even simpler:

Take a data file that contains these entries:

1 2 3
4 5 6
7 8 9

Read this file to place these values into an single array mArray[?] of the proper size.

Give it a try and post code where you try to read this file. You say you are a visual learner so you must write the code to see what is happening. Start with a simple main function, open your data file, read the data file one number at a time into an array. Then print out the array to insure you read the variables correctly.



Jim


I'm gonna call that data file attempt1.dat

So I'm going to place these into a 1x9 matrix. (Note: I'm on my mac, so I can't compile and check at this time)

int main()
{
ifstream indata;
int data[9];

indata.open("attempt1.dat");

if(!indata)
{
cerr << "error" << endl;
return EXIT_FAILURE;
}

indata >> data;

while( !indata.eof() )
{
cout << data << endl;;
indata >> data;
}


indata.close();

cout << "end of file" << endl;

return EXIT_SUCCESS
}



correct?
Was This Post Helpful? 0
  • +
  • -

#13 TheScandal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-April 11

Re: Question: store .dat file into array.. really confused

Posted 26 April 2011 - 02:02 PM

So now I'm on my pc, this is what I wrote, tested it, and it works


#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream indata;
    int data[9];
    size_t i;

    indata.open("attempt1.dat");

    if(indata.fail())
    {
	cout << "error" << endl;
	return EXIT_FAILURE;
    }

    while( !indata.eof())
    {
	for(i = 0; i < 9; ++i)
	{
	    indata >> data[i];
	    cout << data[i] << endl;
	}
    }

    indata.close();
}



it outputs the following in my cmd window,
1
2
3
4
ect...

so now, how do I screw with the first variable, the root of all my problems? lol

like earlier,

3
1 2 3
4 5 6
7 8 9

how do I store the 3 separate?
Was This Post Helpful? 0
  • +
  • -

#14 TheScandal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 25-April 11

Re: Question: store .dat file into array.. really confused

Posted 26 April 2011 - 02:27 PM

sorry, for the multiple posts, I get excited when things begin to click..

so I got my program to work for this data file

3
1 2 3
4 5 6
7 8 9

here is that code

indata >> vert;
    
    for(i = 0; i < 9; ++i)
    {
	for(j = 0; j < 3; ++j)
	{
	    indata >> data[i][j];
	    
	}
    }

    cout << vert << endl;
    cout << data[0][0] << endl;
    cout << data[0][1] << endl;
    cout << data[0][2] << endl;
    cout << data[1][0] << endl;



So my cmd window printed
3
1
2
3
4


ok, now for the next step. The more complex data file.

3
1 2 3
4 5 6
7 8 9
2
10 11
12 13

that 2 is throwing me off. Can I just do

indata >> num; ? (num will store the 2)

right after the for-loops?

edit:
tried that, complete fail... so how do I store the 2?

This post has been edited by TheScandal: 26 April 2011 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,569
  • Joined: 25-December 09

Re: Question: store .dat file into array.. really confused

Posted 26 April 2011 - 02:57 PM

Part of your problem is caused by your first for loop.
    indata >> vert;
    
    for(i = 0; i < 9; ++i)  /// Problem is here.
    {
	for(j = 0; j < 3; ++j)
	{
	    indata >> data[i][j];
	    
	}
    }

    cout << vert << endl;
    cout << data[0][0] << endl;
    cout << data[0][1] << endl;
    cout << data[0][2] << endl;
    cout << data[1][0] << endl;




How many lines does your data file contain?
1 2 3
4 5 6
7 8 9

The first loop should execute once for each line, in this case 3 times.

So using this knowledge the loops should be defined like:

    const int NUMLINES = 3;
    const int NUMROWS = 3
    int indata = 0;
    int num = 0;

    indata >> vert;
    
    for(i = 0; i < NUMLINES; ++i)  /// Problem is here.
    {
	for(j = 0; j < NUMROWS; ++j)
	{
	    indata >> data[i][j];
	    
	}
    }


Notice I replaced the constants with a constant.

Now if you have your number of lines and number or rows correct you should be able to read the next piece of data with indata >> num;

Also how have you defined your data variable?

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2