Results 1 to 5 of 5

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    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
    554
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanked 86 Times in 81 Posts

    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.

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, 19:21
  2. read ini file content and save into array
    By cooper in forum Newbie
    Replies: 8
    Last Post: 14th March 2011, 21:33
  3. QVector array declear
    By zhxys in forum Newbie
    Replies: 8
    Last Post: 2nd February 2011, 09: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, 20:13
  5. read from file into array
    By obad in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2010, 08: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
  •  
Qt is a trademark of The Qt Company.