Read the file from the current directory using ifstream and QtCreator

I am using Qt Creator for a simple C ++ project without Qt libraries.

I am trying to open a file as follows:

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

or

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

But this does not work with the file name, as in Visual Studio, I have to pass the full path to open the file ...

Does anyone know why? and how can I reference the current directory without using Qt libs?

+4
source share
3 answers

You can use QDir::current() to make sure the working directory is what you want. Without Qt, you can use the TomA related solution .

Options allow you to configure it to run the application from the IDE.

+2
source

It will be; your code is ok. But, like the other answers, you have to make sure that you use it in the directory that you think.

In the left pane, select "Projects", then (from the tab at the top) "Run Settings", and it will show you where it launches the executable file in the "Working Directory" field. I think the default is the directory above the release and debug folders.

+2
source

The difference between Visual Studio and Qt Creator may be that

  • Each runs a binary program file in a different subdirectory of your project structure.
  • We copy the text.txt file as part of your project into the same output directory as the binary, the other does not.

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

0
source

Source: https://habr.com/ru/post/1391268/


All Articles