PDA

View Full Version : How to read matrix from text file in QT



Abraão Állysson
24th September 2014, 02:18
Hello, I was wondering how would the code below into QT

/*
content a MATRIX.txt
1 2 3
4 5 6
7 8 9 */



#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
using namespace std;


int main()
{

// declaring a POINTER FOR MATRIX to be dynamically allocated
char **m;
//char matriz[3][5];


//by declaring file handle variable to a txt file
ifstream txtFile;
int l,c,i,j;

//READ FILE.txt
txtFile.open("MATRIX.txt");



//read that LINE e COLUMN the matrix ;
txtFile >> l >> c;

//allocate a memory for matrix
m = (char **) malloc( l * sizeof(char *) );
for(int nl=0;nl<l;nl++){
m[nl]=(char *) malloc(c*sizeof(char *));
}

//open content a FILE.txt
for ( i=0; i<l; i++)
{

for(j=0; j<c; j++)
{
txtFile >> m[i][j];

}

}

cout << "the Matrix read file have LINE and COLUMN: \t " << l<< "X" <<c;
cout << "\n content of the matrix in the file: "<<endl;
for (int i=0;i<l;i++)
{

for(int j=0;j<c;j++)
{
cout << m[i][j];

}
printf("\n");
}




//fecha arquvivo
txtFile.close();

system("pause");

}

d_stranz
24th September 2014, 03:06
In the first place, your file "MATRIX.TXT" does not have the structure you are trying to read. There is no count of rows (l) and columns (c) at the top of the file. In the second place, you are allocating your internal matrix data structure with char type, not int type, so the ifstream:: operator>>() will not read it correctly. Your m[i][j] values will be characters (e.g. '1', '2', etc.), not integers (1, 2, 3 ...). Finally, you have a memory leak, because you don't delete the arrays you have malloc'ed.

To translate the C code to C++ and Qt, you would use QTextStream to replace ifstream. You could use QVector (QVector<int>) to replace your array of arrays structure:




QTextStream textFile( "matrix.txt", QIODevice::ReadOnly | QIODevice::Text );
int l;
int c;

textFile >> l >> c; // Assumes you fix your file so it contains #rows and #columns on the first line
QVector<int> matrix( l * c, 0 );

for ( int nl = 0; nl < l; ++nl )
{
for ( int nc = 0; nc < c; ++nc )
{
textFile >>matrix[ nc + nl * c ];
}
}



Alternatively, with an iterator:



QTextStream textFile( "matrix.txt", QIODevice::ReadOnly | QIODevice::Text );
int l;
int c;

textFile >> l >> c; // Assumes you fix your file so it contains #rows and #columns on the first line
QVector<int> matrix( l * c, 0 );

QVector<int>::iterator it = matrix.begin();
QVector<int>::iterator eIt = matrix.end();
while ( it != eIt )
{
textFile >> *it++;
}



Or, if you don't put the number of rows and columns at the top of the file, but just read it the way it was in your original post:




QTextStream textFile( "matrix.txt", QIODevice::ReadOnly | QIODevice::Text );
QVector<int> matrix;

while ( !textFile.atEnd() )
{
int value;
textFile >> value;
matrix.push_back( value );
}



(But of course, you don't know the "shape" of the matrix now - you don;t know the number of rows or columns, so it is just a flat array of ints).

Learn to use "CODE" tags when you copy code to a post here. Click "Go Advanced", then click the "#" icon to insert the tags. Put your source code in between them.

Abraão Állysson
24th September 2014, 16:35
Hello, I was wondering how would the code below into QT

//Code tested in dev c++


#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
using namespace std;

int matriz();


int main()
{

matriz();


system("pause");
}

int matriz() {
// declaring a POINTER FOR MATRIX to be dynamically allocated
int **m;
//int matriz[3][5];


//by declaring file handle variable to a txt file
ifstream txtFile;
int l,c,i,j;

//READ FILE.txt
txtFile.open("matrix.txt");


//read that LINE e COLUMN the matrix ;
txtFile >> l >> c;

//allocate a memory for matrix;

m = (int **) malloc( l * sizeof(int *) );
for(int nl=0;nl<l;nl++){
m[nl]=(int *) malloc(c*sizeof(int *));
}
//READ content a FILE.txt
for ( i=0; i<l; i++)
{

for(j=0; j<c; j++)
{
txtFile >> m[i][j];

}

}

cout << "the Matrix read file have LINE and COLUMN: \t " << l<< "X" <<c;
cout << "\n content of the matrix in the file: "<<endl;
for (int i=0;i<l;i++)
{

for(int j=0;j<c;j++)
{
cout << (n[j][TAM-1-i]=m[i][j];;

}
printf("\n");
}
//close file
txtFile.close();
}



(INPUT) content a matrix.txt



35 35
1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1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 1 1 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 0 0 1 0 0 1 1 1 1
1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 0 1 0 0 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1






OUTPUT



11111111111111101111111111111111111
11111111110111000011111111111111111
11111111110010000001111111111111111
11111111110000000000011111111111111
11111111100000000000000000111111111
11111110000000000000000000000000001
11111100011110000000000000000000000
11111001111111000000000000000000001
11110011111111000000000000000000011
11100111110011000000000000000000111
11001111110010000111110000000001111
11011111100010011111111000000011111
10011111101100111111111110000111111
10111110011101111111111111100011111
00111000011001111111111111110011111
00110000011011111111111111110011111
00101000110011111111111111110011111
01001100110011111111111111110011111
01011100110011111000011111110011111
01011100110011110000001111110011111
01011001111011110000001111110011111
01011001111011110000001111110001111
00010001111001111111111111100000111
00011001111100111111111111000000001
00011000111110001111111100000000000
10001000111111100000000000000000001
10000000111000110000111111110001111
11010000110000000111111111110011111
11001000110000011111111111100011111
11100100010000011100000111000011111
11110011010000011000000110011111111
11111001100000011000001100111111111
11111110010010111111110011111111111
11111111000001011100000111111111111
11111111110000000000011111111111111






need to make these changes with several .TXT(I have to open a string of txt l,as the figure below )

10635

d_stranz
25th September 2014, 01:21
Didn't you read my reply? I gave you three different ways to read your text files using Qt classes. The first two ways will read the file you posted above.

If you need to read more than one file, you can supply the names as command line arguments, and use the optional argc, argv arguments to the main() function. Make a loop that counts over argc, retrieves each file name from the command line, then opens and reads it using the code already given. Remember that argv[0] contains the name of the program, not the first file name, so start with argv[1].

I'm not going to write this code for you. You should be able to read the Qt documentation and do this yourself.

wysota
27th September 2014, 23:09
Hello, I was wondering how would the code below into QT

If the code works why do you want to change it? Qt is standard C++ so standard C++ routines don't need to be ported to Qt.