PDA

View Full Version : [Solved] Qt Creator and ifstream doesn's work



jabaa
28th August 2014, 13:31
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:


#include <fstream>
#include <iostream>

class myClass{
private:
public:
myClass(){};
void doSomething(std::string path){
std::string gl;
std::ifstream file;

file.open(path.c_str(),std::ios_base::in);

while(std::getline(file,gl)){
std::cout << gl << std::endl;
}
file.close();
};
};

int main(){
myClass myclass;
myclass.doSomething("text.txt");
std::ifstream file;
return 0;
}


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:



#include <fstream>
#include <iostream>

class myClass{
private:
public:
myClass(){};
void doSomething(std::string path){
//std::string gl;
};
};

int main(){
myClass myclass;
myclass.doSomething("text.txt");
std::ifstream file;
return 0;
}




#include <fstream>
#include <iostream>

class myClass{
private:
public:
myClass(){};
void doSomething(std::string path){
std::string gl;
};
};

int main(){
myClass myclass;
myclass.doSomething("text.txt");
//std::ifstream file;
return 0;
}




#include <iostream>
#include <fstream>

class myClass{
private:
public:
myClass(){};
void doSomething(std::string path){
std::string gl;
};
};

int main(){
myClass myclass;
myclass.doSomething("text.txt");
std::string path;
std::ifstream file;
return 0;
}


But this code doesn't work



#include <fstream>
#include <iostream>

class myClass{
private:
public:
myClass(){};
void doSomething(std::string path){
std::string gl;
};
};

int main(){
myClass myclass;
myclass.doSomething("text.txt");
//std::string path;
std::ifstream file;
return 0;
}


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.

wysota
28th August 2014, 14:53
Have you tried debugging it?

jabaa
28th August 2014, 15:32
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.