Jump to content
  • Advertisement
Sign in to follow this  
wacman

File input - relative file paths

This topic is 4768 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 am trying to open a text file with fstream::open but it will only work with an absolute file path (C:\Game\Data\data.dat). I need it to work with a relative path (Data\data.dat). Have I just misunderstood how this function works? Is this simply a limitation of MSVC or am I doing something wrong? Also, what is the difference between fstream::open and fopen? Is there any benefit in using one over the other?

Share this post


Link to post
Share on other sites
Advertisement
Make sure your "working directory" is where the exe is,
under vc2003, look under debugging/working directory and
enter $(TargetDir)

Oh yeah, sometimes vc "forgets" the settings... dunno why, might
be when another teammember checks in a modified workspace.

/R

Share this post


Link to post
Share on other sites
unix uses slashes (/)
windows backslashes(\)

although i haven t encountered a problem with slashes in windows yet when working with relative pathnames

does the vc++ oder the vc++ standard library convert to windows pathnames internally?

Share this post


Link to post
Share on other sites
in MSVC++ these worked for me.

EXAMPLE 1:
exe is here: C:/folder1/myexe.exe
i want a text file in a folder of my exe

fstream inFile("mytext.txt", ios::in)

it will open C:/folder1/mytext.txt

EXAMPLE 2:
exe is here: C:/folder1/myexe.exe
i want a text file in a folder 1 level above my exe

fstream inFile("../mytext.txt", ios::in)

it will open C:/mytext.txt

EXAMPLE 3:
exe is here: C:/folder1/myexe.exe
i want a text file in the folder folder2

fstream inFile("/folder2/mytext.txt", ios::in)

it will open C:/folder1/folder2/mytext.txt

hope i helped
-Lordoftools

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
unix uses slashes (/)
windows backslashes(\)

although i haven t encountered a problem with slashes in windows yet when working with relative pathnames

does the vc++ oder the vc++ standard library convert to windows pathnames internally?


While true, windows also accepts forward slashes (/), therefore it makes sense to just use forward slashes all of the time. However, the OP's problem is most likely due to his active directory not lining up with his expectations.

Share this post


Link to post
Share on other sites
If you use the \ in an ascii string, the compiler thinks it is an escape sequence. So if you give it the path and filename "Data\data.dat" won't work because \d is not a valid character. Try using "Data\\data.dat"

Also, are you using mfc? Bill Gates likes to change the current working directory if you use certain mfc objects like CFileDialog. You can use the function _getcwd to find what the current working directory is when you try to open the file.

Share this post


Link to post
Share on other sites
Instead of changing the project-settings, you might put sth like
this in the beginning (win32)

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, int nCmdShow)
{
// avoid errors when vs7-ide 'forgets' targetdir
String cmdLine (GetCommandLine());
String cmdPath ( String (cmdLine, cmdLine.rfind ('\\') ));
SetCurrentDirectory(cmdPath.c_str());

... of course the point about mfc changing the current-working-dir
somewhere is still valid :)

Share this post


Link to post
Share on other sites
i think i will enhance my implementation to convert all backslashes to forward slashes this way i stay platform independent as well

Share this post


Link to post
Share on other sites
Yes, forward slashes are nice. I think all the other suggestions are good ones. Did you try prefixing your relative path with a ./ ?

Share this post


Link to post
Share on other sites
As an answer to the second question OP asked (which nobody seems to have noticed), there is quite a significant difference between fopen and fstream::open.

fopen is a C standard library function, and it gives you a FILE pointer.

std::fstream is a C++ standard library class, and it is recommended that you should use this in preference to fopen in a C++ program.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!