How to get this file path correct in C++?

Started by
12 comments, last by demonkoryu 13 years, 11 months ago
I mean using this here located at the top of the Explorer.exe: I wanted to try getting something (SDL related stuffs, not important about it though) by making a path "linking" from a folder called "Project Creation" to "Project Creation/sprite" folder. However, I don't know how C++ understands this "Parent-Child Directory Paths", or something. Is it one of these?: C_Path = "./sprite/"; C_Path = "../sprite/"; C_Path = ".\\sprite\\"; Or am I getting something wrong? Thanks in advance. Awaiting replies. :)
Advertisement
The C++ standard library uses the conventions of your OS, that means you could use either path.

What's your exact problem? Is it maybe that the current directory is not your project root directory, when you execute your program? Or is it something else?
When running from within the Visual Studio IDE, the working directory is the location where the source files are stored. (i.e. Projects\Project Creation\Project Creation)
But if you run the exe file outside the IDE, the working directory is the location where you ran the exe from. So your paths will be wrong when running from outside the IDE, unless you do something about it. If there's a simple and effective solution, I'd like to hear about it.
@taz - you can alter your project settings to set the working directory to be the same as whatever it is when you run it not from within the IDE.

Also, you may be interested in this function (GetCurrentDirectory) erhaps?

http://msdn.microsoft.com/en-us/library/aa364934%28VS.85%29.aspx

(:
Easy, what you describe is the default behavior. Go to the projects properties and change the working directory to whatever you want it to be.

Another option is to set up a post build event to copy your files to a new location so that the path is correct outside of the ide.
I guess I didn't make it clearer. I apologize.

I don't move my project files outside of its own Projects folder.

Instead, I create a new folder, and put things inside it. I want my program to find the files in that new folder created in the root of the project folder that contains the project SLN file.



The first box is the folder I created inside my project folder. I wanted my program to be able to load something from there. Using SDL libraries and IMG_Load(), I need the path of that object. Which is why I was asking if the pathing syntax is correct or not.

The second box proves that the current folder shown is the Projects folder.

The third box is the object I need my program to load with.

The correct way would be: ../sprite/ because even though your project is in the same folder as the "sprite" folder, the executable created goes to the "Debug" folder. You would need to step back one folder from "Project Creation\Debug" (to "Project Creation"), which is what the ".." does. Then you add the "/sprite/" because that's the folder you want.
It returns 1 instead of loading the object, and quits the program early.

In <string.h>, does "c_str()" uses that pathing syntax?
What are you talking about? <string.h> does not provide the std::string class, of which .c_str() is a member, and it has nothing to do with file paths anyway - it only exists to adapt std::string objects to functions that require a const char*.
This is all a bit dodgy because Visual Studio by default will set the working directory to be the same as the source directory when run from within the IDE, but the working directory will be the exe directory if run from outside.

Unless someone runs your application from the command line, in which case the working directory will be whatever directory they are in.

IIRC, the standard open and save dialogs can change the working directory as well.

You can also mess with the working directory via the Start In property of shortcuts.

In a nutshell, you can't really depend upon the working directory being anything predictable. I use GetModuleFilename to get the path to my executable and extract the directory, then build all my paths that are relative to the executable relative to that.

This topic is closed to new replies.

Advertisement