Results 1 to 5 of 5

Thread: QVector or 2D Array to read out from a file

  1. #1
    Join Date
    Apr 2020
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default QVector or 2D Array to read out from a file

    Hi,

    i got a file which holds some lines like this:

    19,sunshine live,0x1234
    19,radio bob, 0xabcd
    19,sputnik,0x1a2b

    and so on..

    My program reads this file and my goal is to output only the station name (2 entry in each line) in a QListWidget. Anyway the other 2 entries in each line should be hold in a vector or array, to use them later.

    So far I got this, but it doesn't work right:

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. QFile file(pfad);
    4. if(!file.open(QFile::ReadOnly | QFile::Text)){
    5. QMessageBox::warning(this,"..","keine datei gefunden");
    6. return;
    7. }
    8.  
    9. QTextStream in_file(&file);
    10. QString text;
    11.  
    12. QVector<QVector<QString> > vectorOfVectorsOfStrings;
    13. QVector<QString> zeile;
    14.  
    15. while (!in_file.atEnd()) {
    16. text = in_file.readLine();
    17. QStringList split_text = text.split(",");
    18.  
    19. for (int i = 0; i < 3; i++){
    20.  
    21. zeile.push_back(split_text.at(i));
    22. vectorOfVectorsOfStrings.push_back(zeile);
    23. }
    24. }
    25.  
    26. file.close();
    27.  
    28. for(int i = 0; i < vectorOfVectorsOfStrings.size(); i++)
    29. {
    30. for(int j = 0; j < vectorOfVectorsOfStrings[i].size(); j++)
    31. {
    32. ui->listWidget->addItem(vectorOfVectorsOfStrings[i][1]);
    33. }
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    The program is crashing with message [i] out of range. So i tried to set a fixed value just to so the output, and then my ListWidget was showing several times sunshine live. I guess the error is somewhere in here:

    Qt Code:
    1. while (!in_file.atEnd()) {
    2. text = in_file.readLine();
    3. QStringList split_text = text.split(",");
    4.  
    5. for (int i = 0; i < 3; i++){
    6.  
    7. zeile.push_back(split_text.at(i));
    8. vectorOfVectorsOfStrings.push_back(zeile);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,521
    Thanked 278 Times in 273 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector or 2D Array to read out from a file

    I would add a check on the number of items in split_text list.

  3. #3
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    502
    Thanked 75 Times in 71 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVector or 2D Array to read out from a file

    The program is crashing with message [i] out of range.
    Then use a debugger and take a look at the backtrace why the assertion occours.

  4. #4
    Join Date
    Apr 2020
    Posts
    14
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QVector or 2D Array to read out from a file

    So I added a debug to the while statement, to see what is hold in vectorOfVectorsOfStrings

    Qt Code:
    1. QVector(QVector("19"), QVector("sunshine live"), QVector("0x1234"), QVector("19"), QVector("dlf kultur"), QVector("0x9876"), QVector("19"), QVector("dlf1 kultur"), QVector("0x9876"), QVector("19"), QVector("dlf2 kultur"), QVector("0x9876"), QVector("19"), QVector("dlf3 kultur"), QVector("0x9876"), QVector("19"), QVector("dlf4 kultur"), QVector("0x9876"), QVector("19"), QVector("dlf5 kultur"), QVector("0x9877"))
    To copy to clipboard, switch view to plain text mode 

    I expected something like

    Qt Code:
    1. QVector(QVector("19", "sunshine live", "0x1234"), QVector("19", "dlf kultur", "0x9876")........)
    To copy to clipboard, switch view to plain text mode 

    Am I wrong and it already runs well, or am I right?


    Added after 1 10 minutes:


    Just to complete it, solution:

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3.  
    4. QFile file(pfad);
    5. if(!file.open(QFile::ReadOnly | QFile::Text)){
    6. QMessageBox::warning(this,"..","keine datei gefunden");
    7. return;
    8. }
    9.  
    10. QTextStream in_file(&file);
    11. QString text;
    12.  
    13. QVector<QVector<QString>> vectorOfVectorsOfStrings;
    14. QVector<QString> zeile;
    15.  
    16. while (!in_file.atEnd()) {
    17. text = in_file.readLine();
    18. qDebug() << text;
    19.  
    20. QStringList split_text = text.split(",");
    21. qDebug() << split_text;
    22.  
    23. QVector<QString> zeile;
    24.  
    25. zeile.push_back(split_text.at(0));
    26. zeile.push_back(split_text.at(1));
    27. zeile.push_back(split_text.at(2));
    28. qDebug() << zeile;
    29.  
    30. vectorOfVectorsOfStrings.push_back(zeile);
    31. qDebug() << vectorOfVectorsOfStrings;
    32.  
    33. }
    34.  
    35. file.close();
    36.  
    37. for(int i = 0; i < vectorOfVectorsOfStrings.size(); i++)
    38. {
    39. ui->listWidget->addItem(vectorOfVectorsOfStrings[i][1]);
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by vitalic; 22nd April 2020 at 21:10.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,695
    Thanks
    259
    Thanked 753 Times in 743 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QVector or 2D Array to read out from a file

    For one thing, you have declared the variable "zeile" twice, once outside your loop, and again inside it. The inside declaration hides the outer one.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. How to get a pointer for 2D array of QVector?
    By tfmp in forum Qt Programming
    Replies: 16
    Last Post: 19th September 2012, 20:21
  2. read ini file content and save into array
    By cooper in forum Newbie
    Replies: 8
    Last Post: 14th March 2011, 22:33
  3. QVector array declear
    By zhxys in forum Newbie
    Replies: 8
    Last Post: 2nd February 2011, 10:17
  4. read a .txt file and store it in a double array
    By fatecasino in forum Newbie
    Replies: 5
    Last Post: 3rd December 2010, 21:13
  5. read from file into array
    By obad in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2010, 09:26

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.