Results 1 to 5 of 5

Thread: How to read matrix from text file in QT

  1. #1
    Join Date
    Sep 2014
    Posts
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default How to read matrix from text file in QT

    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");

    }

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to read matrix from text file in QT

    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:

    Qt Code:
    1. QTextStream textFile( "matrix.txt", QIODevice::ReadOnly | QIODevice::Text );
    2. int l;
    3. int c;
    4.  
    5. textFile >> l >> c; // Assumes you fix your file so it contains #rows and #columns on the first line
    6. QVector<int> matrix( l * c, 0 );
    7.  
    8. for ( int nl = 0; nl < l; ++nl )
    9. {
    10. for ( int nc = 0; nc < c; ++nc )
    11. {
    12. textFile >>matrix[ nc + nl * c ];
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    Alternatively, with an iterator:

    Qt Code:
    1. QTextStream textFile( "matrix.txt", QIODevice::ReadOnly | QIODevice::Text );
    2. int l;
    3. int c;
    4.  
    5. textFile >> l >> c; // Assumes you fix your file so it contains #rows and #columns on the first line
    6. QVector<int> matrix( l * c, 0 );
    7.  
    8. QVector<int>::iterator it = matrix.begin();
    9. QVector<int>::iterator eIt = matrix.end();
    10. while ( it != eIt )
    11. {
    12. textFile >> *it++;
    13. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. QTextStream textFile( "matrix.txt", QIODevice::ReadOnly | QIODevice::Text );
    2. QVector<int> matrix;
    3.  
    4. while ( !textFile.atEnd() )
    5. {
    6. int value;
    7. textFile >> value;
    8. matrix.push_back( value );
    9. }
    To copy to clipboard, switch view to plain text mode 

    (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.
    Last edited by d_stranz; 24th September 2014 at 03:18.

  3. #3
    Join Date
    Sep 2014
    Posts
    2
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to read matrix from text file in QT

    Hello, I was wondering how would the code below into QT

    //Code tested in dev c++
    Qt Code:
    1. #include <iostream>
    2. #include <stdlib.h>
    3. #include <iomanip>
    4. #include <fstream>
    5. using namespace std;
    6.  
    7. int matriz();
    8.  
    9.  
    10. int main()
    11. {
    12.  
    13. matriz();
    14.  
    15.  
    16. system("pause");
    17. }
    18.  
    19. int matriz() {
    20. // declaring a POINTER FOR MATRIX to be dynamically allocated
    21. int **m;
    22. //int matriz[3][5];
    23.  
    24.  
    25. //by declaring file handle variable to a txt file
    26. ifstream txtFile;
    27. int l,c,i,j;
    28.  
    29. //READ FILE.txt
    30. txtFile.open("matrix.txt");
    31.  
    32.  
    33. //read that LINE e COLUMN the matrix ;
    34. txtFile >> l >> c;
    35.  
    36. //allocate a memory for matrix;
    37.  
    38. m = (int **) malloc( l * sizeof(int *) );
    39. for(int nl=0;nl<l;nl++){
    40. m[nl]=(int *) malloc(c*sizeof(int *));
    41. }
    42. //READ content a FILE.txt
    43. for ( i=0; i<l; i++)
    44. {
    45.  
    46. for(j=0; j<c; j++)
    47. {
    48. txtFile >> m[i][j];
    49.  
    50. }
    51.  
    52. }
    53.  
    54. cout << "the Matrix read file have LINE and COLUMN: \t " << l<< "X" <<c;
    55. cout << "\n content of the matrix in the file: "<<endl;
    56. for (int i=0;i<l;i++)
    57. {
    58.  
    59. for(int j=0;j<c;j++)
    60. {
    61. cout << (n[j][TAM-1-i]=m[i][j];;
    62.  
    63. }
    64. printf("\n");
    65. }
    66. //close file
    67. txtFile.close();
    68. }
    To copy to clipboard, switch view to plain text mode 


    (INPUT) content a matrix.txt

    Qt Code:
    1. 35 35
    2. 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 1 1 1 1 1
    3. 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1
    4. 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1
    5. 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 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1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 0 1 0 0 1 1 1 1 1
    34. 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
    35. 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
    36. 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
    To copy to clipboard, switch view to plain text mode 



    OUTPUT
    Qt Code:
    1. 11111111111111101111111111111111111
    2. 11111111110111000011111111111111111
    3. 11111111110010000001111111111111111
    4. 11111111110000000000011111111111111
    5. 11111111100000000000000000111111111
    6. 11111110000000000000000000000000001
    7. 11111100011110000000000000000000000
    8. 11111001111111000000000000000000001
    9. 11110011111111000000000000000000011
    10. 11100111110011000000000000000000111
    11. 11001111110010000111110000000001111
    12. 11011111100010011111111000000011111
    13. 10011111101100111111111110000111111
    14. 10111110011101111111111111100011111
    15. 00111000011001111111111111110011111
    16. 00110000011011111111111111110011111
    17. 00101000110011111111111111110011111
    18. 01001100110011111111111111110011111
    19. 01011100110011111000011111110011111
    20. 01011100110011110000001111110011111
    21. 01011001111011110000001111110011111
    22. 01011001111011110000001111110001111
    23. 00010001111001111111111111100000111
    24. 00011001111100111111111111000000001
    25. 00011000111110001111111100000000000
    26. 10001000111111100000000000000000001
    27. 10000000111000110000111111110001111
    28. 11010000110000000111111111110011111
    29. 11001000110000011111111111100011111
    30. 11100100010000011100000111000011111
    31. 11110011010000011000000110011111111
    32. 11111001100000011000001100111111111
    33. 11111110010010111111110011111111111
    34. 11111111000001011100000111111111111
    35. 11111111110000000000011111111111111
    To copy to clipboard, switch view to plain text mode 




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

    aaaa.jpg
    Last edited by Abraão Állysson; 24th September 2014 at 16:45.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to read matrix from text file in QT

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to read matrix from text file in QT

    Quote Originally Posted by Abraão Állysson View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to read the text of a pdf file?
    By Momergil in forum Newbie
    Replies: 4
    Last Post: 22nd January 2016, 10:45
  2. Qt 5 Read a Text File With Delimiters
    By te777 in forum Qt Programming
    Replies: 4
    Last Post: 9th November 2014, 14:25
  3. Read unicode text from file
    By Boron in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2012, 15:27
  4. Read from text file
    By kulsekarr in forum Newbie
    Replies: 3
    Last Post: 8th June 2012, 12:11
  5. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 08:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.