Results 1 to 11 of 11

Thread: Qt Prog skip the file reading block

  1. #1
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    1

    Default Qt Prog skip the file reading block

    Hi, I am a new new Qt student. haven't quite captured Qt's feature. First I say thank you all for your viewing this post.

    Today I have written such codes:

    Qt Code:
    1. #include <QtGui>
    2. #include <iostream>
    3.  
    4. int main(int argc, char* argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QFile file("text.txt");
    9. file.open(QIODevice::ReadOnly);
    10. QTextStream in(&file);
    11.  
    12. std::cout << "Before the while loop." << '\n';
    13.  
    14. QString Line;
    15. while (! in.atEnd())
    16. {
    17. std::cout << "a Line" << std::endl;
    18. Line = in.readLine();
    19.  
    20. }
    21. std::cout << "Finished the loop" << std::endl;
    22.  
    23. file.close();
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    the text.txt has four lines.

    So I am hopefully expecting the console output like:

    Before the while loop.
    a Line
    a Line
    a Line
    a Line
    Finished the loop

    But after I went into the release folder and type the program name, the result was disappointing:
    Before the while loop.
    Finished the loop

    Something is more strange, I use Code::Blocks. So if I compiled the program there and then hit its run button, the result was right, four "a Line" with the both head and tail statements.

    And I haven't forgotten added in the .pro file such a line
    CONFIG += console

    So my question are:
    What is wrong with my program? why make.exe -> in console type the program doesn't show the four "a line"? But Code::Blocks could build the program correctlly and run it reasonally?

    Thank you all!!!
    Last edited by dgg32; 10th December 2008 at 17:37.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Prog skip the file reading block

    first of all if don't need event processing you don't need to create QApplication.
    second, try to use next snippet of code
    Qt Code:
    1. ...
    2. QFile file("in.txt");
    3. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    4. return;
    5.  
    6. QTextStream in(&file);
    7. while (!in.atEnd()) {
    8. QString line = in.readLine();
    9. ...
    10. }
    11. ...
    To copy to clipboard, switch view to plain text mode 
    btw, this code from the Assistant, see QFile
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    1

    Default Re: Qt Prog skip the file reading block

    Hi thanks for your reply. I tried your snippet just now. The .exe in the release folder showed nothing, while the .exe in debug showed just like before: before and finished the loop. I don't have any clue.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Prog skip the file reading block

    put your txt with exe file or specify full path to file.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    1

    Default Re: Qt Prog skip the file reading block

    Quote Originally Posted by spirit View Post
    put your txt with exe file or specify full path to file.
    have already done those before came here.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Prog skip the file reading block

    what does QFile::open return?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    1

    Default Re: Qt Prog skip the file reading block

    Quote Originally Posted by spirit View Post
    what does QFile::open return?
    it returned true.

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Prog skip the file reading block

    show your pro-file.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    1

    Default Re: Qt Prog skip the file reading block

    Quote Originally Posted by spirit View Post
    show your pro-file.
    QMake Code:
    1. ######################################################################
    2. # Automatically generated by qmake (2.01a) Mi 10. Dez 14:25:13 2008
    3. ######################################################################
    4.  
    5. TEMPLATE = app
    6. TARGET =
    7. DEPENDPATH += .
    8. INCLUDEPATH += .
    9. CONFIG += console
    10.  
    11. # Input
    12. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 2nd January 2009 at 23:19.

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt Prog skip the file reading block

    so, looks like problem with determination of file path. try to set full path to file, i.e.
    "c:/text.txt"
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. The following user says thank you to spirit for this useful post:

    dgg32 (10th December 2008)

  12. #11
    Join Date
    Dec 2008
    Posts
    6
    Thanks
    1

    Thumbs up Re: Qt Prog skip the file reading block

    Quote Originally Posted by spirit View Post
    so, looks like problem with determination of file path. try to set full path to file, i.e.
    "c:/text.txt"
    Man, Qt rocks, you rocks

Similar Threads

  1. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  2. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

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.