4

I'm using Qt Creator for a plain C++ project without the Qt libraries.

I'm trying to open a file like this:

fopen("text.txt", "r");

or

ifstream fin;
fin.open("text.txt");

But it doesn't work with just the filename like in Visual Studio, I have to pass the full path for it to open the file...

Anybody knows why is that? and how can I refer to the current directory without using Qt libs?

nbro
  • 15,395
  • 32
  • 113
  • 196
Chrono
  • 59
  • 1
  • 3
  • 4
    Are you sure your "current directory" is actually what you think it is? – Kerrek SB Jan 16 '12 at 14:39
  • 1
    To get the current directory without Qt, see this question: http://stackoverflow.com/questions/143174/c-c-how-to-obtain-the-full-path-of-current-directory – Tomas Andrle Jan 16 '12 at 14:55

3 Answers3

2

You can use QDir::current() to check wether the working directory is what you want it to be. Without Qt you can use the solution TomA linked to.

The run settings allow you to configure it for running the application from the IDE.

Community
  • 1
  • 1
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
2

It will; your code is fine. But as other answers allude, you need to make sure you're running it in the directory you think you are.

On the left panel select "Projects" then (from the tabs at the top) "Run Settings" and it will show you where it runs the executable from in the field labeled "Working directory". I think by default it's the directory above the release and debug folders.

Samuel Harmer
  • 4,264
  • 5
  • 33
  • 67
  • Thanks :D, you are right, I was using the shadow build option, so it was building on another folder. – Chrono Jan 16 '12 at 16:27
0

The difference between Visual Studio and Qt Creator may be that

  1. Each starts the program binary in a different subdirectory of your project structure.
  2. One does copy the text.txt file as part of your project to the same output directory as the binary, the other does not.

Try to get the current directory using this and then see if it actually contains the file.

Community
  • 1
  • 1
Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92