Results 1 to 5 of 5

Thread: Strange problem

  1. #1
    Join Date
    Dec 2009
    Location
    Poland
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face Strange problem

    Hello, i have a problem with writing to char** array from file.
    I couldn't explain why, first n-1 lines is copied with no problem, but in last n line it outputs only last, and previous.

    http://pastebin.com/m4a12d893


    Example file :
    1223 John
    343 Dude
    233 Zordo

    I'm doing it becouse I want sort file by numbers in line beggining.
    Maybe someone could give me some other way to do it in simply way?

  2. #2
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange problem

    I don't know how fast would that be, but you could try using
    QMap<int, QString>

    Read one line to QString, catch the number at the beginning of the line (using QRegExp for example) and use it as a key in QMap. Than simply iterate over your QMap.

    Maybe Qt Gurus know better solution

    BTW next time paste your code to the post and use CODE tags.
    Last edited by calhal; 18th December 2009 at 10:36. Reason: updated contents
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  3. #3
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Strange problem

    Qt Code:
    1. char *tmp=(char*)(txt.readLine().toStdString().c_str());
    2. tab[i]=tmp;
    To copy to clipboard, switch view to plain text mode 

    I'm not a C++ expert but I think you should not do things like this. c_str() will give you a pointer internal object's storage containing null terminated string.
    I'm not sure if the pointer you keep in tab[0] will be still valid when you are on next iteration after another
    Qt Code:
    1. txt.readLine().toStdString().c_str()
    To copy to clipboard, switch view to plain text mode 
    .

    Try what I wrote in a previous post - it's definitely more Qt-ish
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  4. The following user says thank you to calhal for this useful post:

    pavel (18th December 2009)

  5. #4
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Strange problem

    Is it a constraint of the assignment that you must use C-style strings and arrays? Neither is appropriate for this problem, unless that requirement has been imposed.

    If you are not required to use C-style strings, use std::string or QString instead.

    If you are not required to use arrays, use containers (either Standard Template Library containers or Qt containers) instead.

    If you must use an array of C-style strings, then calhal’s observation applies: when c_str() is applied to a temporary, the value it returns is guaranteed valid only during the statement that contains the call. (When applied to a non-temporary, it’s guaranteed valid only so long as the object to which it was applied exists and no non-const member function has been called on that object.) It’s only by chance that the code you have is working at all. Instead, you must save the QString or std::string, allocate a character buffer of the appropriate size (QString::length + 1), then copy the character data into the buffer (e.g., with strcpy). Don’t forget to delete the buffers in your clean-up code.

    Much simpler and better, though, would be to use QStrings in containers.

  6. #5
    Join Date
    Dec 2009
    Location
    Poland
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Strange problem

    Qt Code:
    1. #include <QString>
    2. #include <QMap>
    3. #include <QRegExp>
    4. #include <QMapIterator>
    5.  
    6.  
    7. int main () {
    8.  
    9. QFile file("../MOVE QWidget/gamefiles/hiScores.txt");
    10. QMap<int,QString> fileMap;
    11.  
    12. if (file.open(QIODevice::ReadOnly)) {
    13.  
    14. QTextStream txt(&file);
    15. QRegExp rx("(^[0-9]+)");
    16.  
    17. while ( !txt.atEnd() ) {
    18. QString line =txt.readLine();
    19. rx.indexIn(line);
    20. int number = rx.cap(1).toInt();
    21. fileMap.insert(number,line);
    22. }
    23.  
    24. QMapIterator<int,QString> i(fileMap);
    25. i.toBack();
    26. while ( i.hasPrevious()) {
    27. i.previous();
    28. std::cout<<i.key()<<" ";
    29. }
    30.  
    31. }
    32. file.close();
    33. }
    To copy to clipboard, switch view to plain text mode 


    Ok, thanks a lot for your help!
    As you see, i go through first advice of calhal.
    code snippet of course in 'CODE' tags ;-)

Similar Threads

  1. MySQL driver - Very strange problem. PLEASE HELP ME. Thanks.
    By diego in forum General Programming
    Replies: 15
    Last Post: 3rd March 2011, 02:33
  2. Replies: 7
    Last Post: 13th August 2009, 17:11
  3. Replies: 2
    Last Post: 2nd June 2009, 15:57
  4. MySQL driver - Very strange problem. PLEASE HELP ME. Thanks.
    By diego in forum Installation and Deployment
    Replies: 0
    Last Post: 2nd June 2009, 02:56
  5. strange problem passing a vector
    By mickey in forum General Programming
    Replies: 15
    Last Post: 22nd November 2007, 10:46

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.