3

I am attempting to read in an n × n matrix from a file and then store that matrix in a one dimensional array. I would also like to store the value for n. I have looked into various approaches but can't seem to apply them to what I'm trying to achieve.

This is what I have so far but I'm unsure what to put into the while loop.

/*READ IN THE IMAGE MATRIX FROM THE FILE*/

String lineA;
ifstream imFile;
imFile.open("imageMatrixDefined.txt");
if(imFile.fail()){
    cerr << "File cannot be found or opened" << endl;
    exit(1);
}

if(imFile.is_open(){
    cout << "file opened successfully!" << endl;
    while(!imFile.eof()){    
    }
}

The input file could look like the following:

1    2    3
2    3    1
3    3    2

where a tab separates the elements. Any suggestions would be greatly appreciated as I'm new to C++.

Tony Davis
  • 67
  • 1
  • 10

3 Answers3

0

Arrays have a fixed size.
You have to get the value for n first before initializing an array.

It's good to double check if you would like to store that matrix in a one dimensional array or a two dimensional array. If it is a one dimensional array, checking how the matrix is stored in a one dimensional array is good. Some stores it first row, second row, ..., and nth row, and some stores it first column, second column, ..., and nth column.

The matrix is n × n, so the number of columns is equal to the number of rows.
The file stores one row at a time. Getting the value of n is not difficult.

The while loop is critical, but before the while loop, getting the value of n is the first step of solving the problem.

When the value of n is gotten, the array is easy to be initialized. You can read the line one by one from the file in the while loop, get each matrix element by using the delimiter as a tab character, and store the matrix element in the array.

0

You have to bind a custom input stream with your input file. Say your input file is "in.txt", and it looks the same as you have specified.

The following code then reads from the file, stores it in a 1D array and prints the resulting matrix in matrix form:

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ifstream myfile;
  myfile.open ("in.txt");

  cout << "Reading from a file.\n";
  int arr[10];
  int k = 0;
  for (int i = 0; i < 3; ++i)
  {
    for (int j = 0; j < 3; ++j)
    {
        myfile >> arr[k];           //Read it in a 1D array   
        cout << arr[k] << "  ";
        ++k;
    }
    cout << "\n";
  }

  myfile.close();

  return 0;
}

Output:

Reading from a file.
1  2  3  
2  3  1  
3  3  2  
Roel Van de Paar
  • 2,111
  • 1
  • 24
  • 38
amarVashishth
  • 847
  • 3
  • 12
  • 26
0

Reading a 2D array to a file. Input is assumed to be comma separated, one row per line and x amount of columns per row, each column separated by a tab (\t) character. No EOL (end of line) tab's required, just a newline (\n). Remember to set rows and columns variables correctly. Code is tested to work correctly.

using namespace std;
#include <iostream>
#include <fstream>

int main() {
  int rows=10, columns=10;

  ifstream f("input.txt");
  int input[rows][columns];
  for (int i=0; i<rows; i++){
    for (int j=0; j<columns; j++){
      f >> input[i][j];
    }
  }
  f.close();

  // Here you can add processing of the file, for example, print it:

  cout << "Input:" << endl;
  for (int i=0; i<rows; i++){
    for (int j=0; j<columns; j++){
      cout << input[i][j] << "  ";
    }
    cout << endl;
  }
  return 0;
}
Roel Van de Paar
  • 2,111
  • 1
  • 24
  • 38