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;
}
#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;
}
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:
#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;
}
To copy to clipboard, switch view to plain text mode
#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;
}
To copy to clipboard, switch view to plain text mode
#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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
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;
}
#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;
}
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.
Bookmarks