Hello All,

I'm creating a program that reads a file.dat and I need to input that in a matrix, it can be 2x2,3x3,4x4 or 5x5. I'm not exactly sure how to do that.
The file.dat looks like this:

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

Notice the spaces..

The first number in the line, for example 2 1 2 3 5, 2 means that its going to be a matrix 2x2.

Thanks in advance,

Doug

Recommended Answers

All 7 Replies

use fscanf() function and some logic,,,,i did same thing using this only

int n;
//read the first number from the file
int* x=new int[n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
//read element element from file and store
//in matrix
x[j]=elem;

So, right now my function looks like this:

for(int i=0;i<Size;i++)
for(int j=0;j<Size;j++)
{
myfile >> MagSq[j];
cout << MagSq[j] << endl;
}

Is that what you said?

I did some operations and I came with this,

function::Input_Values()
{
	ifstream myfile ("file.dat");
	stringstream input;

	ifstream myfile ("Magic.dat");
	stringstream input;
	for(int i=0;i<Size;i++)
	for(int j=0;j<Size;j++)
	{
		while(!myfile.eof())
		myfile.getline(MagSq[i][j], Size);
		cout << MagSq[i][j] << endl;
	}
}

MagSq is in my header file and Size as well, so

Compile error: c error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'int' to 'char *'

However I get a weird std error..

I might paraphrase that deeply nested attempt to fill the array like this: "read the whole array (somehow) into each and every cell in the two dimensional array".

here you are the complete code

//the file content
2 6 5 8 4
3 6 5 4 7 8 9 2 5 8
2 6 9 7 4
4 7 8 9 4 5 6 7 8 9 1 2 3 2 5 8 7



[/
//the matrix.cpp

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

void showMatrix(int[] ,int );


void main()
{
    //define the size of the matrix
    int Size(0);

    //define the height and the width  of the matrix
    int Height(0),Width(0);

    //define a pointer to the created matrix
    //and initaly by NULL
    //it will be treated as one dimiensionl array 
    //as we will see
    int* mat=NULL;

    //refered to file in current directory
    string MyFile="matrix.dat";

    //for entring user path

//  cout<<"Please enter the file location and name : ";
//  cin>>MyFile;

    //create stream oject to maniulate with file
    ifstream MyReader;

    //open the file in input mode and binary formate
    MyReader.open(MyFile.c_str(),ios::in|ios::binary);


    //for opening error
    if(!MyReader.is_open())
    {
        cerr<<".....File Open Failed !!!!!"<<endl;
        exit(1);
    }


    //still reading from file till reach it's end
    while(!MyReader.eof())
    {
        //take the size of the matrix
        MyReader>>Size;

        //define the height and the width of the matrix
        Height=Size;
        Width=Size;

        //create one dimitional array of size  "Size*Size" as
        //we treate with it as two dimitional array
        mat=new int[Size*Size];

        //reading each elemen of the matrix
        for(int R=0;R<Height;R++)
            for(int C=0;C<Width;C++)
                //this formate to acess one dimentional array 
                //by index of two dimentiona array
                MyReader>>mat[R*Width+C];

        //to show the matrix in beatiful form
        showMatrix(mat,Size);
    }

}


void showMatrix(int x[],int Size)
{
    int Height=Size;
    int Width=Size;

    cout<<endl<<endl;

    for(int R=0;R<Height;R++)
    {
        cout<<"|";
        for(int C=0;C<Width;C++)
            cout<<x[R*Width+C]<<" ";
        cout<<"|"<<endl;
    }
    cout<<endl<<endl;
}

]

i will be happy if i help you 
good bye[code=c++]
[/
//the matrix.cpp


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


void showMatrix(int[] ,int );



void main()
{
//define the size of the matrix
int Size(0);


//define the height and the width  of the matrix
int Height(0),Width(0);


//define a pointer to the created matrix
//and initaly by NULL
//it will be treated as one dimiensionl array
//as we will see
int* mat=NULL;


//refered to file in current directory
string MyFile="matrix.dat";


//for entring user path


//  cout<<"Please enter the file location and name : ";
//  cin>>MyFile;


//create stream oject to maniulate with file
ifstream MyReader;


//open the file in input mode and binary formate
MyReader.open(MyFile.c_str(),ios::in|ios::binary);



//for opening error
if(!MyReader.is_open())
{
cerr<<".....File Open Failed !!!!!"<<endl;
exit(1);
}



//still reading from file till reach it's end
while(!MyReader.eof())
{
//take the size of the matrix
MyReader>>Size;


//define the height and the width of the matrix
Height=Size;
Width=Size;


//create one dimitional array of size  "Size*Size" as
//we treate with it as two dimitional array
mat=new int[Size*Size];


//reading each elemen of the matrix
for(int R=0;R<Height;R++)
for(int C=0;C<Width;C++)
//this formate to acess one dimentional array
//by index of two dimentiona array
MyReader>>mat[R*Width+C];


//to show the matrix in beatiful form
showMatrix(mat,Size);
}


}



void showMatrix(int x[],int Size)
{
int Height=Size;
int Width=Size;


cout<<endl<<endl;


for(int R=0;R<Height;R++)
{
cout<<"|";
for(int C=0;C<Width;C++)
cout<<x[R*Width+C]<<" ";
cout<<"|"<<endl;
}
cout<<endl<<endl;
}


]

i will be happy if i help you
good bye

Thanks so much!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.