Sign in to follow this  

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

This topic is 2790 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. :)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
@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

(:

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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*.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Thank you, Aardvajk.

I have found my answer in the link provided from you.

The syntax for it is "..\\<Folder>\\<Folder>\\<etc.>"

Needs two periods, then two backslashes for every backslash you see in the absolute path of a file for it to be a correct form of the relative path.

Link.

Share this post


Link to post
Share on other sites
Quote:
Original post by tom_mai78101
Thank you, Aardvajk.

I have found my answer in the link provided from you.

The syntax for it is "..\\<Folder>\\<Folder>\\<etc.>"

Needs two periods, then two backslashes for every backslash you see in the absolute path of a file for it to be a correct form of the relative path.

Link.


Though you gain nothing by using backslashes in file-paths in C++: clicky.

Many (relative) filepaths are even portable amongst unix and windows if you use forward slashes.

Share this post


Link to post
Share on other sites

This topic is 2790 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this