2
votes

At the moment I am using QtCreator 2.8.1 based on Qt 5.1.1 under Ubuntu Linux. I do both, Qt Development and coding pure C++ projects. Because I don't want to install a second heavyweight IDE like Eclipse for latter I'd like to use QtCreator for that.

Up to now I created a new C++ Project (without Qt) by clicking on File->New->C/C++ Project (without Qt)->C++-Project. So now coding works fine. But with my first build I discovered a huge problem with my main.cpp

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream infile("file1.in");
    string line;

    while(infile >> line) {
        cout << line << endl;
    }

    cout << "hello world" << endl;

return 0;
}

When I run this within QtCreator I see only "hello world" on the console, but nothing more (file1.in contains more than 20 lines of raw text). Now the strange thing. After compiling this with a little g++ main.cpp I see all my 20 lines of text and "hello world".

Does anybody have a clue why this happens? I thought I have to change the compiler in QtCreator, but this attempt wasn't successfully.

1
If you're running the executable in from QtCreator I'm guessing the file you're reading is not in the working directory of the executable, so the file is never read. Since you have no error handling for that situation, your software just prints hello world. To verify add the full path of the file1.in to the constructor of ifstream. - Arne Kjetil Andersen
You can also check if you have disabled shadow build to be sure that Arne is right. - Kakadu
...or add error handling/reporting - Frank Osterfeld
Yes, you're absolutely right. Qt build the file in a seperate folder... My mistake. When I change my build configuration and QtCreator builds it in my working directory everythings works. Thanks! - Nick Lehmann
Note that if you disable shadow build, and then re-enable it, make sure you do make clean distclean (easiest from command line) in the source directory, to make sure compiler doesn't end up using stale files from there at some situation. Also, I'd prefer using shadow build, it allows doing debug and release builds without needing to clean in between (especially useful, if you have more targets than just plain debug and release). - hyde

1 Answers

0
votes

QtCreator by default use shadow builds, it means you executable will be build in separated folder. In your code you use relativ path to the file:

ifstream infile("file1.in");

simply change it fullpath or make sure your file1.in exist in the shadow build folder for your project.

ifstream infile("C:\\file1.in");