Results 1 to 12 of 12

Thread: QTextStream readLine() problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextStream readLine() problem

    Hi guys..
    I have some problems reading a file.
    always I run the program, this crash in the same position of file.
    I used gdb to debug and I had this error:

    #0 0x00002aaaabd4c40b in memcpy () from /lib64/libc.so.6
    #1 0x00002aaaaaf9c172 in QString::append (this=0x639260, str=@0x7fff529efe10) at /usr/include/bits/string3.h:52
    #2 0x00002aaaaafe4277 in QTextStreamPrivate::fillReadBuffer (this=0x6391d0, maxBytes=-1) at ../../src/corelib/tools/qstring.h:255
    #3 0x00002aaaaafe4f25 in QTextStreamPrivate::scan (this=0x6391d0, ptr=0x7fff529f3f08, length=0x7fff529f3f1c, maxlen=0, delimiter=QTextStreamPrivate::EndOfLine) at io/qtextstream.cpp:696
    #4 0x00002aaaaafe5a81 in QTextStream::readLine (this=<value optimized out>, maxlen=0) at io/qtextstream.cpp:1584
    #5 0x0000000000401ed2 in main (argc=2, argv=0x7fff529f41b8) at main.cpp:53


    the problem, I think is in.readLine() but I don't know why.

    Could you help me?

    Qt Code:
    1. if(sql_file.open(QIODevice::ReadOnly | QIODevice::Text))
    2. {
    3. QTextStream in(&sql_file);
    4. QString line;
    5. int pos = 0;
    6.  
    7. do {
    8. line = in.readLine(); <------- problem here
    9. qDebug() << "pos: " << pos << " length: " << line.length();
    10.  
    11. ++pos;
    12. //process_line(line);
    13. } while (!line.isNull());
    14.  
    15. sql_file.close();
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    What do you do with pos? Do you use it to seek in the file?
    Can you show more code responsible for reading and navigating in file?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    yes, of course.
    The "pos" var is only to see something and always crash in the same position.

    Qt Code:
    1. QCoreApplication a(argc, argv);
    2.  
    3. QStringList args = QCoreApplication::arguments();
    4.  
    5. QRegExp rxArgPath("^--sql_path=(.*)");
    6.  
    7. for (int i = 1; i < args.size(); ++i)
    8. {
    9. //qDebug() << args.at(i);
    10.  
    11. if(rxArgPath.indexIn(args.at(i), 0) == 0)
    12. {
    13. if(!rxArgPath.cap(1).isEmpty())
    14. {
    15. QFile sql_file(rxArgPath.cap(1));
    16.  
    17. if(sql_file.exists())
    18. {
    19. if(sql_file.open(QIODevice::ReadOnly | QIODevice::Text))
    20. {
    21. qDebug() << "reading file " << rxArgPath.cap(1);
    22.  
    23. QTextStream in(&sql_file);
    24. QString line;
    25. int pos = 0;
    26.  
    27. do {
    28. line = in.readLine();
    29. qDebug() << "pos: " << pos << " length: " << line.length();
    30.  
    31. ++pos;
    32. } while (!line.isNull());
    33.  
    34. sql_file.close();
    35. }
    36. }
    37. else
    38. {
    39. qWarning() << "Er: the file " << rxArgPath.cap(1) << "not exists";
    40. }
    41. }
    42. else
    43. {
    44. qWarning() << "Er: Add the path --sql_path";
    45. }
    46. }
    47. }
    48.  
    49. return a.exec();
    To copy to clipboard, switch view to plain text mode 

    thanks in advance

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTextStream readLine() problem

    Ahh... this all thing is in your main...
    I think that working with QIODevice needs a running event loop (but I am not sure).
    Put this in to a class, and run this code after the event loop is running, that will be a good idea even if its not the problem.
    Maybe some one else can confirm if this is the problem though.

    EDIT:
    [re-edit] I take back what I said here.
    Last edited by high_flyer; 6th January 2011 at 15:00.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Problem quitting QThread that uses readLine
    By DiamonDogX in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2010, 16:43
  2. Qtextstream::readline()
    By dawn_to_dusk_ in forum Qt Programming
    Replies: 4
    Last Post: 3rd August 2008, 17:58
  3. readLine problem
    By gQt in forum Qt Programming
    Replies: 10
    Last Post: 29th February 2008, 09:35
  4. QTextStream, input and readLine()
    By kramed in forum Newbie
    Replies: 6
    Last Post: 1st September 2007, 23:54
  5. QTextStream problem
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 19th June 2007, 09:50

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.