Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read file into 2d array 1

Status
Not open for further replies.

MorganGreylock

Programmer
Jan 30, 2001
223
US
I have a text file that I'm trying to read into an array. The file consists of, on the first line, the # of lines, then followed by data values, 4 per line.

so like this:

5
1.0 2.0 6.0 7.0
5.0 3.0 2.0 5.0
1.0 2.0 6.0 7.0
2.0 9.0 1.0 2.4
5.0 2.0 6.5 1.4

I want to read this data in an X by 4 array, where X is the value on the first line (here its 5, but it could be anything). I've successfully opened the file for read, read in the first line (which I call data_size), and now i'm trying to dynamically allocate an array of size (in this case 5x4) to hold this data, and it's giving me problems. Here is what I have so far:

in the main declaration segment:
Code:
// ... other declarations
FILE *stream;
float *input_data = NULL;

later:

Code:
void read_input() {
int i;

	i = 0;
	stream = fopen("data.txt","r");
	if( stream == NULL )
    	  printf( "The file data.txt was not opened\n" );
	else
   {
	fscanf(stream,"%i",&data_size);			
	printf("data size = %d\n",data_size);
	input_data = new float[data_size][4]; 

	while(fscanf(stream,”%f %f %f %f”, &input_data[i][0],&input_data[i][1],&input_data[i][2],&input_data[i][3]) != EOF)
	{
		i++;
	}
	fclose(stream);
   }

}

Something's not working, and quite honestly I'm not too sure where. I know the inital fopen and first fscanf is working, but the rest of it is really suspect.

Any help is very much appreciated. I've searched for solutions, but nothing really fit my particular situation that I could find.

Thanks in advance,
MG
 
Hi,

Use STL, it will ease your life. See this working code below :
Code:
#include <fstream>
#include <vector>
using namespace std;

void read_input ()
{
  ifstream in_stream ("data.txt");

  int data_size;
  in_stream >> data_size;//note that you don't need this info.
  
  vector< float* > input_data;

  int line_id = 0;
  while (!in_stream.eof ())
    {
      input_data.push_back (new float[4]);
      for (int j = 0; j <= 3; j++)
	in_stream >> input_data[line_id][j];

      line_id++;
    }
}

int main ()
{
  read_input ();

  return 0;
}

This code assumes that "data.txt" is ok : 4 numbers per line.

--
Globos
 
What is the general way to declare a multi-dimensional array dynamically? I really didn't want to use the STL for this project if necessary...

Thanks,
MG
 
I'd say globos's code is the way to go, but I think the problem with your code was that you declared your input_data as a single pointer when it should have been a double pointer. i.e. float** input_data = NULL;
Then once you know that you need 5 lines, do:
Code:
input_data = new float*[data_size];
and allocate the row elements as:
Code:
input_data[index] = new float[4];
 
That seemed to work, but when I try to read the values in, I get an unhandled exception error... heres what I've got so far: (i was initialized to 0 earlier in the code)

Code:
while((fscanf(stream,"%f %f %f %f", &input_data[i][0],&input_data[i][1],&input_data[i][2],&input_data[i][3]) != EOF))
	{
		i++;
	}

I don't have a lot of experience with fscanf, but that's basically what I was told to use...

Thanks again for any help... it is very much appreciated.
MG
 
How did you create & allocate the input_data array? It will blow up like that if you're using a NULL pointer.
 
I created it just as you had suggested:
Code:
float** input_data = NULL;

...

input_data = new float*[data_size];
for(i=0;i<data_size;i++)
{
  input_data[i] = new float[4];
// I assumed this is where the multi-dimensionality comes 
// into being, but I'm not sure by any means
}

Perhaps I'm missing something else here? All I want to do is read in 'data_size' lines, with 4 values per line into an array (separated by whitespace). The values are all float (except for the data_size, which is an int, obviously).

Incidentally, do you know of anywhere that has some good documentation or tutorials about pointers (and double pointers)? It's been a while since I've used them, and I've never used double pointers, and I want to read up about them.

Thanks,
MG
 
Hi,

I suggest to do :

Code:
typedef float* FloatPointer;
//...

void read_input ()
{
//...
  FloatPointer* input_data = new FloatPointer[data_size];
//...
  for (i=0; i < data_size; i++)
  {
    input_data[i] = new float[4];
    //...
  }
}

--
Globos
 
Step through your code in the debugger and see if the array has the right values that you are expecting. Also see if it's throwing the exception on the fscanf() or somewhere else...
 
Thanks everyone, I finally convinced them to go the vector route, and it worked just fine.

MG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top