Read a file from current directory using ifstream and QtCreator - c++

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?

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.

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.

The difference between Visual Studio and Qt Creator may be that
Each starts the program binary in a different subdirectory of your project structure.
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.

Related

VS Code unable to detect Standard C++ Libraries

I started using VS Code, and after messing around with it I kind of managed to make it detect Windows and Direct3D SDK's with the c_cpp_properties.json, but I'm failing to make the Standard Library work. So, if I do:
#include <string>
#include <vector>
It throws me an error just like this:
//Include file not found in include path
I've searched all over the web and didn't find any clue, so here I am! Strangely enough, if I just create a new .cpp file in an empty window/editor, it works. But the moment I 'load' the folder that file is in, then it fails. So, this is really driving me crazy.
I'm using Windows 10, with .NET 4.6.2, the 2015 Visual C++ Redist, and the Windows 10 SDK, all blazing new installs from today. My ultimate goal is to port a project I made in VS2013 to GNU/Linux, so I'm trying to make things work step by step.
Thanks a lot beforehand!
EDIT: Compiling with g++ works just fine, even though VS Code complains. This is what happens.
VS Code need to locate the include libraries.
First of all locate where g++ is located. You mentioned that it works fine. It's an .exe file (windows). So you may find g++ directory in path settings. view path variables.
Now after getting g++.exe directory you may easily find a file names string in nearby folders or parent folders. After successfully locating it copy its full path.
Now back in VS Code put cursor over green underline and you should see a bulb. Click it and in the options you will see option Edit "includePath" setting or Update "browse.path" setting. Select it and a file will open named c_cpp_properties.json
Now in that file locate "name": "win32". In the include path option paste the directory name of string file like this and you are good to go.
In vscode go to file>preferences>settings then select edit in settings.json (This can be hard to find, certain settings have this option by them, others do not. There is probably a better way to access this file, but I don't know it)
This will open up the settings.json file, where you can add the line:
"C_Cpp.default.includePath": ["C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\include"]
(or whatever your include path is) This will add that include path for all projects, or only the current workspace depending on if you open the settings.json file for "User Settings" or for "Workspace Settings"

Visual Studio looking for a file in the Desktop directory instead of the Project Directory [duplicate]

I've got an a project (native C++ compiled to .exe) that runs fine outside of VS 2010, but inside of VS (with or without the debugger) it gets stuck trying to find a text file located in the same bin folder as it.
Any ideas as to why this would happen? My hunch is that VS messes with where the code looks when trying to open a file, but I don't know enough details to correct this.
Some details:
My .exe calls a function from a .dll I wrote earlier, which in turn tries to find a text file specifying that function's parameters. For convenience I've placed all of these files in the same folder, so finding the text file wouldn't be a problem.
Before opening the file, the function checks that it exists using:
PersistentAssert(Utility::FileExists(Filename), "Parameter file not found");
In VS 2010 this line causes "Parameter file not found" to display, but outside of VS the program manages to find the file.
When launching an executable from visual studio by default it uses the project file directory as the current working directory. You can change it in Configuration Properties -> Debugging -> Working Directory.
However I don't think it's correct behavior to search your programs datafiles etc from the current working directory. Instead your program should find out the directory of the executable and find the files in that directory.
Processes have the concept of a "current directory", which may or may not be the same directory as where the .exe file is located. It sounds like when you run your program inside VS, the current directory is something other than where your .exe is.
Somewhere in the project settings, there should be a place where you can choose what directory will be the current directory when your program starts inside the debugger. Set that to the same location as your .exe and you should be good.

How does working directory work?

In Visual Studio, you are provided the option to change the working directory. I understand how to use it, but I do not know how it works.
How does the working directory setting work in relation to the complication of the application?
For example, if a program tries to open a file using relative directories (like e.g. "dir\\file.ext") then that relative directory is from the programs working directory. The working directory doesn't have to be the directory where the program is located, and the working directory can be changed from inside the program with e.g. the SetCurrentDirectory function.
Another example: Lets say you are starting a program from a command prompt window, and you are in the directory C:\some\folder\. The program is located in D:\other\folder\. Since the current directory when you start the program is C:\some\folder\, the programs working directory will be that as well.

How to specify the directory of a .config file in C++ code?

I have a C++ program that I built using Visual Stuidos. It has to read some variables from a .config file. I notice that when I run the program from VS (i.e. I open the VS solution and press the "Start Without Debugging" button), it can't find the .config file unless it's in the "release" or "debug" directory. However, I'd like to be able to run the program from the VS solution, while keeping the .config file in the same directory as the .sln file. Is there a way I can set the path of the .config file to this directory, perhaps somewhere in the source code?
Thanks.
I would take advantage of Visual Studios directory macros and set an additional include path in your solution directory, I answered a question previously on this site relating to include directories, so here is a diagram I created:
bigger link here
Don't pay attention to the text on the left side, but the black text on the right side is what you're looking for. $(SolutionDir) is a macro that finds the directory of your solution, and adding this will allow your program to have access to this directory during the program.
You can call GetModuleFileName to get the full path to your exe file. From there you can remove the lowest level (/debug or /release) to get the project path.
That should get you what you asked for, but what you asked for is not a good approach. Putting your config file in your exe directory will encounter problems later on with non-admin user rights. The recommended place for such files can be found by calling SHGetFolderPath, where you can create an accessible folder for your app.
However, I'd like to be able to run the program from the VS solution,
while keeping the .config file in the same directory as the .sln
file.
Just set Project Properties|Debugging|Working Directory to $(SolutionDir). Or any directory you want.

Where do data files go so the Microsoft Visual C++ 2008 debugger can find them?

I am writing code that opens an istream object on a file specified by the user. I want to be able to run the program in the debugger and just type the filename (eg data.txt) at the prompt, not the whole path. I haven't worked out how to do this inside the IDE so I have been saving my .txt file to the debug folder and running the .exe file, but that means I can't step through the program. How do I make it work inside the IDE instead? Thanks.
you can set the working path of the executable (project properties->Debugging->Working Directory), which leads the debugger to start the executable with that path as working directory. This has the advantage that if you set the same path for all your configurations (Debug/Release/...), you only need 1 data.txt on your entire system, which is especially nice if you want to change data.txt or it's name.
I am not sure I understand exactly the problem - is it that your data file is part of the project, but is not in the executable folder when you access it, or is it that the datafile is at another location? If the former, and the data file is part of the project, right-click on the file, and set the Build Action property to "Content". That way, it will get copied to the bin/debug folder where the executable runs when you debug.