Results 1 to 3 of 3

Thread: [Solved] Qt Creator and ifstream doesn's work

  1. #1
    Join Date
    Aug 2014
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default [Solved] Qt Creator and ifstream doesn's work

    Hello,

    I have a problem with Qt Creator and std::ifstream. I discussed the problem at http://qt-project.org/forums/viewthread/46737/ but since I didn't get any solution, I will try it at this forum.

    I use Qt Creator as my standard C++ Editor. So the problem has nothing to do with Qt. First I will show an example code:
    Qt Code:
    1. #include <fstream>
    2. #include <iostream>
    3.  
    4. class myClass{
    5. private:
    6. public:
    7. myClass(){};
    8. void doSomething(std::string path){
    9. std::string gl;
    10. std::ifstream file;
    11.  
    12. file.open(path.c_str(),std::ios_base::in);
    13.  
    14. while(std::getline(file,gl)){
    15. std::cout << gl << std::endl;
    16. }
    17. file.close();
    18. };
    19. };
    20.  
    21. int main(){
    22. myClass myclass;
    23. myclass.doSomething("text.txt");
    24. std::ifstream file;
    25. return 0;
    26. }
    To copy to clipboard, switch view to plain text mode 

    This code is ok. Qt Creator compiles it and runs it. Everything is great. But when I try to strat the application outside of Qt Creator, it crashes without error message. When I compile the code in the console with mingw, it is also ok. So I tried to change the compiler in Qt Creator to the extern g++ (gcc version: 4.8.1). When I compile the code in Qt Creator and start it from outside, everything is ok. But I can't start it from Qt Creator. It crashes. When I kick out std::ifstream of the code, everything is ok. But I need ifstream in this case. I worked on many projects with Qt Creator without any problems. Everything compiles and runs. The Qt libs are added to the system environment. This is my first code with ifstream and the first time, I have a problem with Qt Creator. I hope, someone can help.

    Here are some more codes to show the strange behaviour:

    These codes are ok:

    Qt Code:
    1. #include <fstream>
    2. #include <iostream>
    3.  
    4. class myClass{
    5. private:
    6. public:
    7. myClass(){};
    8. void doSomething(std::string path){
    9. //std::string gl;
    10. };
    11. };
    12.  
    13. int main(){
    14. myClass myclass;
    15. myclass.doSomething("text.txt");
    16. std::ifstream file;
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <fstream>
    2. #include <iostream>
    3.  
    4. class myClass{
    5. private:
    6. public:
    7. myClass(){};
    8. void doSomething(std::string path){
    9. std::string gl;
    10. };
    11. };
    12.  
    13. int main(){
    14. myClass myclass;
    15. myclass.doSomething("text.txt");
    16. //std::ifstream file;
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <iostream>
    2. #include <fstream>
    3.  
    4. class myClass{
    5. private:
    6. public:
    7. myClass(){};
    8. void doSomething(std::string path){
    9. std::string gl;
    10. };
    11. };
    12.  
    13. int main(){
    14. myClass myclass;
    15. myclass.doSomething("text.txt");
    16. std::string path;
    17. std::ifstream file;
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 

    But this code doesn't work

    Qt Code:
    1. #include <fstream>
    2. #include <iostream>
    3.  
    4. class myClass{
    5. private:
    6. public:
    7. myClass(){};
    8. void doSomething(std::string path){
    9. std::string gl;
    10. };
    11. };
    12.  
    13. int main(){
    14. myClass myclass;
    15. myclass.doSomething("text.txt");
    16. //std::string path;
    17. std::ifstream file;
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 

    I can reproduce this strange behaviour on any Win7 Pro 64 bit machine. What's going on here? I tried this code on a Raspberry Pi with Raspbian and there it works fine from inside and outside Qt Creator.
    Last edited by jabaa; 28th August 2014 at 16:00.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Creator and ifstream doesn's work

    Have you tried debugging it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2014
    Posts
    2
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Qt Creator and ifstream doesn's work

    When I start the code out of Qt Creator, it runs without error. So I don't know, how to debug it with Qt Creator. When I start it outside of Qt Creator and wait after it crashes, I can start debugging with MS Visual Studio. But the only message is "Unhandled exception at 0x74B0CB49 in ifstream.exe: 0x0000005: access violation while reading at position 0x0000000". On the bottom I can read something like: "Stack frame libstdc++-6.dll!6fcd68a() is incorrect or missing".

    So I really don't know, how to debug this application. I'd like to know, if someone else can reproduce this error, since I reproduced it on different Win7 Pro 64 bit machines.


    Added after 16 minutes:


    I think, that I unterstand the problem. I have two different, incompatible mingw on my system disturbing each other. When I compile the code with Qt Creator, g++ from Qt Creator is used. When I run the code from Qt Creator, the libstdc++-6.dll from Qt Creator is linked. When I run the application from outside Qt Creator, the wrong libstdc++-6.dll is linked.

    I read on the internet, that the header file for fstream from Qt Creator's mingw are different to other mingw's header files. So the library are different too.

    I copied the libstdc++-6.dll from Qt Creator into my project folder and everything works. The problem is solved for me. Thanks.
    Last edited by jabaa; 28th August 2014 at 15:32.

Similar Threads

  1. ifstream failing, weird behavior
    By BettaUseYoNikes in forum Qt Programming
    Replies: 3
    Last Post: 24th August 2011, 23:17
  2. Shortcut doesn't work
    By stella1016 in forum Qt Programming
    Replies: 1
    Last Post: 28th May 2009, 02:37
  3. QRegExp doesn't seem to work as it should
    By thomaspu in forum Qt Programming
    Replies: 3
    Last Post: 21st December 2007, 07:49
  4. possible bug with ifstream
    By ChasW in forum Qt Programming
    Replies: 5
    Last Post: 3rd February 2007, 07:41
  5. setWindowOpacity doesn't work!
    By nupul in forum Qt Programming
    Replies: 5
    Last Post: 21st April 2006, 18:28

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.