Results 1 to 10 of 10

Thread: qstringlist output to file

  1. #1
    Join Date
    Dec 2010
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qstringlist output to file

    ok, I have a QStringList that I am attempting to write to a file

    Read_Request_Time.at(i) = "109,120,085";


    if (Read_Request_Time.at(i).size() > 0)
    out << "Read_Request" << "\t" << Read_Request_Time.at(i) << "\n";

    the write causes a runtime error and crashed the application.

    I am opening the file correctly, other writes succeed.

    What is wrong with this code? I don't see an error?

    thanks

    John

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: qstringlist output to file

    I think that your using incorrectly the at() member function (at(i) returns a const T < in out case is a const QString> so you can't assign to that)

    But post more code, so that we can see what is wrong.

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: qstringlist output to file

    Make sure that i is in range 0 <= i < list.count() , you'll get failed assertion otherwise:
    Qt Code:
    1. inline const T &QList<T>::at(int i) const
    2. { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 27th February 2011 at 18:41. Reason: typo

  4. #4
    Join Date
    Dec 2010
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstringlist output to file

    i is in range of the QStringList

    Read_Request_Time.at(i) = "123,456,789"

    Opening the file for writing:

    QFile fp(fileName);
    if (!(fp.open(QIODevice::WriteOnly|QIODevice::Text)))
    exit(-1);

    QTextStream out(&fp);

    then I am doing:

    if (system_name.at(i).size() > 0)
    out << "System_Name" << "\t" << system_name.at(i) << "\n";

    with numerous QStringLists SUCCESSFULLY!! ???

    until I get to:

    if (Read_Request_Time.at(i).size() > 0)
    out << "Read_Request" << "\t" << Read_Request_Time.at(i) << "\n";

    this write cause an application error and crashes. and Read_Request_Time.at(i) does contain valid data. "123,456,789"

    Curious!!

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: qstringlist output to file

    Curious!!
    More curious for me is how did you manage to compile this one:
    Qt Code:
    1. Read_Request_Time.at(i) = "123,456,789"
    To copy to clipboard, switch view to plain text mode 
    I can't compile this code ( as expected, because QList::at returns non-assignable const QString& ):
    Qt Code:
    1. list.append("a string");
    2. list.at(0) = "other string"; // error: passing 'const QString' as 'this' argument of 'QString& QString::operator=(const char*)' discards qualifiers
    To copy to clipboard, switch view to plain text mode 

    Read_Request_Time.at(i) does contain valid data. "123,456,789"
    How do you know that ? Maybe post more code.

  6. #6
    Join Date
    Dec 2010
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstringlist output to file

    I did not insinuate that my snippet of code was compilable.

    The QstringList Read_Request_Time contains "123,456,789" at the ith location

    as seen by using a debugger by listing the contents of:
    Read_Request_Time.at(i)

    This is not how I loaded data, I was trying to let you know what the variable was.

    Not sure how much more code is needed.
    I show how I open the file
    I show how I am writing data to file successfully.
    I showed where it is failing and
    I showed the contents of the variable when it crashes.

    Never mind. I will figure it out myself.

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: qstringlist output to file

    I did not insinuate that my snippet of code was compilable.

    The QstringList Read_Request_Time contains "123,456,789" at the ith location

    as seen by using a debugger by listing the contents of:
    Read_Request_Time.at(i)

    This is not how I loaded data, I was trying to let you know what the variable was.
    But will you agree that all above informations were missing from previous posts ? That's why we ( I think Zlatomir also ) were confused and focused on wrong part of your code.
    Now we know more.
    If you still want us to help, try this one to know if it's really the write that causes errors:
    Qt Code:
    1. if (Read_Request_Time.at(i).size() > 0){
    2. out << "Read_Request" << "\t" << "123,456,789" << "\n";
    3. }
    To copy to clipboard, switch view to plain text mode 
    or another way, see what debugger says about this one:
    Qt Code:
    1. if (Read_Request_Time.at(i).size() > 0){
    2. const QString str = Read_Request_Time.at(i) ;
    3. out << "Read_Request" << "\t" << str<< "\n";
    4. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Dec 2010
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstringlist output to file

    I don't agree that the information was missing, but I will agree that the message may have been less than concise.

    But anyhow, I already tried hardwiring the varaiable ala.

    if (Read_Request_Time.at(i).size() > 0)
    out << "Read_Request" << "\t" << "123,456,789" << "\n";

    What is interesting is that I changed it to store a local QString ala.

    if (Read_Request_Time.at(i).size() > 0)
    { <==========
    QString temp_string = Read_Request_Time.at(i);
    out << "Read_Request" << "\t" << temp_string << "\n";
    }

    What happens here, is that the app crashes on the first curly bracket (I marked it with <====)

    I then thought about changing the input if statement to:

    if (Read_Request_Time.size() > i) // this is a for loop and i increments.
    {
    QString temp_string = Read_Request_Time.at(i);
    out << "Read_Request" << "\t" << temp_string << "\n";
    }

    with the same behavior. Very curious.

    thanks

    John

  9. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: qstringlist output to file

    But anyhow, I already tried hardwiring the varaiable ala.

    if (Read_Request_Time.at(i).size() > 0)
    out << "Read_Request" << "\t" << "123,456,789" << "\n";
    And it crashed or not ?

    I really want to help, but its difficult to do so as I only see code snippets out of context, which should be ok under some default assumptions ( like: i is local variable in some kind of loop, that cannot be modified elsewhere ( same for Read_Request_Time list ), i is in valid range, only one thread is accessing them at a time ect... ).
    Do you use threads in you code ?
    By looking just at those snippets it's difficult to tell whats wrong, because they look ok.

  10. #10
    Join Date
    Dec 2010
    Posts
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qstringlist output to file

    yes, it crashed.

    Hence my decision to change the conditional statement from

    if (Read_Request_Time.at(i).size() > 0)

    to

    if (Read_Request_Time.size() > i)

    and it crashed on the first curly bracket.

    No threads. i is a local variable, I am able to see the contents of Read_Request_Time.at(i) from within a debugger and it contains valid data.


    Added after 40 minutes:


    I will post here when I find the issue. I see nothing obvious that would cause this error.

    thanks

    John
    Last edited by martial_arts_drummer; 27th February 2011 at 21:05.

Similar Threads

  1. output while executing should be redirected to text file
    By AnithaRagupathy in forum Qt Programming
    Replies: 3
    Last Post: 13th October 2007, 10:33
  2. radiobutton output file
    By nitriles in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2007, 09:04
  3. Uic cannot write output file
    By jobrandt in forum Qt Tools
    Replies: 4
    Last Post: 25th May 2007, 07:55
  4. QStringList straight to file
    By freak in forum Newbie
    Replies: 2
    Last Post: 13th June 2006, 22:38
  5. Replies: 7
    Last Post: 2nd June 2006, 12:48

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.