Results 1 to 3 of 3

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.