Load array from text file using dynamic memory allocation

Sep 30, 2012 at 7:54pm
I am pretty new to c++, and I am having some trouble trying to read a text file into a 2D array using dynamic memory allocation. I have been doing plenty of "googling" and research on how to do this, but nothing I've found is giving me the output I need when I print out the array. Any help would be greatly appreciated.
Sep 30, 2012 at 7:59pm
In case this is not enough information, my text file contains 2 matrices that need to be multiplied. I need to read these matrices into 2 different arrays that are dynamically allocated.
Sep 30, 2012 at 8:25pm
Since you didn't post any sourcecode i can only make assumptions on what is your problem.

The easiest way to load two matrices like this is to use one dimensional dynamically allocated arrays, not two dimensional ones (in my opinion):

 
double value = new double[rows * columns]


So i suggest to avoid two dimensional dynamically allocated arrays if that's what you're trying to do.
Sep 30, 2012 at 8:40pm
Unfortunately, my assignment requires a 2D array, I wish I could avoid it, but it doesn't look like I can.

Here is my source code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream fin;

fin.open ("input1.txt");

int* a = NULL; // Pointer to int, initialize to nothing.

int n; // Size needed for array

fin >> n; // Read in the size

a = new int[n]; // Allocate n ints and save ptr in a.

for (int i=0; i<n; i++) {
a[i] = 0; // Initialize all elements to zero.

fin >> a[i];

cout << a[i] << endl;
}

This is the type of code that I found most when researching this. However, it is not even close to working .. the output I get is 3 and then 1 on another line.

My text file contains:
2 3
1 2 3
4 5 6

3 2
1 2
3 4
5 6
where the top lines are the number of rows and the number of columns ...

Thanks for you help!
Sep 30, 2012 at 10:46pm
Your syntax is incorrect for a 2D array. The syntax should be int array[row_size][column_size]. You need to assign two values for the array.

int row, col;

fin >> row;
fin>> col;

int array[row][col];

That will give the correct size of the 2D array you are looking for. Your code is inputting 2 from the text file and setting that to the size of the one dimensional array. Then your code inputs the two values it reads in next from the file which is 3 and 1.
Sep 30, 2012 at 11:09pm
Thanks so much!! Is that how you do it with the dynamic memory allocation or do I still have to use the pointer and "new"??
Sep 30, 2012 at 11:16pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <iostream>
#include <fstream>
 
using namespace std;
 
int main()
{
   ifstream fin;
 
   fin.open ("input1.txt");

   if ( !fin ) exit( 1 );
 
   int row1, col1; // Sizes needed for array
 
   fin >> row1 >> col1;
 
   int **a = new int *[row1]; 
 
   for ( int i = 0; i < row1; i++ ) 
   {
      a[i] = new int[col1];
   }
 
   for ( int i = 0; i < row1; i++ )
   {
      for ( int j = 0; j < col1; j++ )
      {
         fin >> a[i][j];
      }
   }


   // You do the same for the second array 
 
   return 0;
}
 
Sep 30, 2012 at 11:44pm
Thank you so much for your help ... I really appreciate it!! :))
Topic archived. No new replies allowed.