Results 1 to 5 of 5

Thread: ReadMultiple Files at one time

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default ReadMultiple Files at one time

    First of all, I couldnt figure out how to map or get an array of variable names basically I was trying like QFile file[i] where I would be like 1-3 and i wanted it to be QFile file1, QFile2, QFile3 same with QTextStream because I think if I had that working it would be a lot better.

    Secondly with my Code below I can read in two files, but if I try and read in a 3rd I get an application failure and it wont even open the exe get this error message:
    ASSERT failure in QLIST<T>:perator[]: "index out of range", file C:\QtSDK\Desktop\Qt\4.8.1\mingw\include/Qtcore/qlist.h, line 477. and two mesages saying Invalid parameter passed to C runtime.

    my code is here PS: if i make my two loops i < 2 and comment out the other 3 files it works but I want to read all 5 =/
    Qt Code:
    1. #include <QtGui>
    2. #include "datelistdialog.h"
    3. #include "abitem.h"
    4. #include <sstream>
    5. #include <iostream>
    6. using namespace std;
    7. DateListDialog::DateListDialog()
    8. {
    9.  
    10. QStringList foods;
    11. QStringList dates;
    12. QStringList totalProteins;
    13. QStringList totalFats;
    14. QStringList totalCarbs;
    15. QStringList labels;
    16. labels << tr("Date") << tr("Food") << tr("Total Protein") << tr("Total Carb") << tr("Total Fat");
    17.  
    18. treeWidget = new QTreeWidget;
    19. treeWidget->setColumnCount(5);
    20. treeWidget->setHeaderLabels(labels);
    21.  
    22.  
    23.  
    24. QVBoxLayout *mainLayout = new QVBoxLayout;
    25. mainLayout->addWidget(treeWidget);
    26. setLayout(mainLayout);
    27.  
    28. for(int i = 0; i < 3; i++){
    29.  
    30. string filename = "/";
    31. string type [] = {"date","food","totalProtein","totalCarb","totalFat"};
    32. string number;
    33. stringstream convert;
    34. filename.append(type[i]);
    35. convert << i;
    36. number = convert.str();
    37. filename.append(".txt");
    38.  
    39.  
    40.  
    41. QString filename2 = QString::fromStdString(filename);
    42. QFile i(QDir::homePath() + filename2);
    43. if (i.open(QIODevice::ReadOnly)) {
    44.  
    45. QTextStream input(&i);
    46. input.setCodec("UTF-8");
    47. while(!input.atEnd()){
    48.  
    49. if(filename == "/date.txt"){
    50. dates << input.readLine();
    51. } else if(filename == "/food.txt"){
    52. foods << input.readLine();
    53. } else if(filename == "/totalProtein.txt"){
    54. totalProteins << input.readLine();
    55. } else if(filename == "/totalCarb.txt"){
    56. totalCarbs << in.readLine();
    57. in.readLine();
    58. } else if(filename == "/totalFat.txt"){
    59. totalFats << in.readLine();
    60. in.readLine();
    61. }
    62. }
    63. }
    64. }
    65.  
    66.  
    67.  
    68. for (int i = 0; i < 5; ++i) {
    69. //item->setText(0,"HELLO");
    70. item->setText(0, dates[i]);
    71. item->setText(1, foods[i]);
    72. item->setText(2, totalProteins[i]);
    73. item->setText(3, totalCarbs[i]);
    74. item->setText(4, totalFats[i]);
    75. treeWidget->addTopLevelItem(item);
    76.  
    77. }
    78. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ReadMultiple Files at one time

    my guess is that you get the assert when accessing the QStringLists in line 71 and later. Maybe you should check that they all have 6 entries.

    Also: naming the QFile instance with the same name as the loop variable looks very weird.

    Btw, the easiest way to create filenames based on a patter is QString::arg(). Basically you create a QString using placeholders and then use arg() calls to replace them with data

    Qt Code:
    1. const QString filenamePattern( "/%1%2.txt" );
    2.  
    3. QString filename = filenamePattern.arg( type[i] ).arg( i );
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: ReadMultiple Files at one time

    thanks about the arg that will make life much easier, and btw you were correct about the line 71 and later..I did a .size() check on all of them and found that one of my txt files was only one line instead of 4 like it should have been reason for this was when I was writing the file I forgot the output << endl; (well I had it but it was commented out on that one instead of all the others =p) Thanks again.
    EDIT: the reading of multiple files was working correctly whole time like you thought.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: ReadMultiple Files at one time

    Line 42 should be generating a warning that QFile i masks the int i loop counter (line 28) also.

  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: ReadMultiple Files at one time

    I didn't get any errors but I can rename QFile i to QFile file. tbh I was tyring to make it Qfile 1, Qfile 2, ect by using the i but it just made the variable name i and it works having the same variable name for them all.

    The program is working perfectly now though btw

Similar Threads

  1. Replies: 1
    Last Post: 29th June 2012, 12:38
  2. Replies: 5
    Last Post: 19th November 2010, 02:25
  3. possible open/write two files same time with QFile
    By npotency in forum Qt Programming
    Replies: 4
    Last Post: 15th November 2009, 00:28
  4. changing files lastmodified time
    By ramazangirgin in forum Qt Programming
    Replies: 2
    Last Post: 27th March 2008, 10:04
  5. modification time on files
    By soul_rebel in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 20:51

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.