Archived

This topic is now archived and is closed to further replies.

A trivial C++ syntax question...

This topic is 5673 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

hi all! Hopefully someone with a bit of C++ experience can give me a hand here... I''ve read enough books on the language to make my eyes bleed, but there are still a couple of things that I still dont get: application-relative paths say I want to load a file from the HDD (or save it), but all I know is that it''s relative to the EXE''s path: [EXE]\Maps\Data\Something.txt in VB, for example, I can use the "app.path" variable, and append the rest of the filename from there... but how would I do the same in C++ (or C)??? thanks, Jack;

Share this post


Link to post
Share on other sites
In Win32 applications, you can use the GetCommandLine function. Call that and it will return the application path, the EXE name and it''s command parameters.

e.g.
C:\Folder\Program.exe -command_param_1 -command_param_2

You''ll have to figure out a way to remove the executable name and command parameters from the command line, but it shouldn''t be too difficult. I suggest perhaps strspn or strstr for finding the first occurrence of the application name, assuming that it hasn''t been renamed...It''s possible, but me mind''s gone blank

Share this post


Link to post
Share on other sites
Actually, just tested that and it seems that GetCommandLine() puts speech-marks around the application name so you could use strchr to find the second occurrence of a speech mark.

Just a thought.

Share this post


Link to post
Share on other sites
Check out GetModuleFileName, splitpath and makepath.

I find that using splitpath and makepath is safer than just adding a relative path to the end of another path.

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com

Share this post


Link to post
Share on other sites
What the heck, here is the code :

TCHAR szModuleName[ _MAX_PATH ];
GetModuleFileName( NULL, szModuleName, _MAX_PATH );

TCHAR szDrive[ _MAX_DRIVE ];
TCHAR szDir[ _MAX_DIR ];
_tsplitpath( szModuleName, szDrive, szDir, NULL, NULL );

TCHAR szFullPath[ _MAX_PATH ];
_tmakepath( szFullPath, szDrive, szDir, THE_RELATIVE_PATH, NULL );

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com

[edited by - Blaster on June 4, 2002 6:06:04 PM]

Share this post


Link to post
Share on other sites
That''s all unneccesary, in Win32.

The answer to your question is yes. Windows will search in the folder that the .exe is running from first, and then the windows folder, the system folder, and then the %path% environment variable.

Share this post


Link to post
Share on other sites
daerid : Maybe for loading DLLs, but for a simple fopen I don''t think it''s the case. If there is no dir in the filename, I''m pretty sure it will look in the current directory only.

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com

Share this post


Link to post
Share on other sites
well it''ll almost always be a case of searching in the same folder as the EXE, or one of the sub-folders...

so just searching the folder the program is running from (will it search sub-folders?) should work?

or is it just much safer to use the API calls / command line that was suggested?

Jack;

Share this post


Link to post
Share on other sites
When you run a program, you can specify a starting directory. So the current directory may not be the exe''s dir when the program starts. That can be overcome by specifically setting the current dir from GetModuleFileName but.

I don''t think CreateFile or the likes will look in sub folders. And it shouldn''t either; what if there are two files with the same name, but in two different sub folders?

----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com

Share this post


Link to post
Share on other sites
When the program starts, if the current working directory isn''t the location of the executable, then it''s somebody else''s problem. Still, if you''re really worried, you should set the current working directory to the location of the executable at the beginning of your program and forget about it.

You can use fopen() with relative directories. There''s no point in going to extra effort to give it absolute directories.

Share this post


Link to post
Share on other sites

int main(int argc, char *argv[])
{
...
// obtaining this .EXE path
std::string exePath = argv[0];
...
}


Added with some code... you could... er... do what you intended to do.

[edited by - DerekSaw on June 4, 2002 10:10:25 PM]

Share this post


Link to post
Share on other sites
Here's a real easy way to get the path also...


    
#include <string>

#include <direct.h>

using namespace std;

main()
{
string CurrentWorkingDirectory = _getcwd( NULL,255);

}



[edited by - tonic151 on June 7, 2002 11:19:50 AM]

Share this post


Link to post
Share on other sites