I am very much a part time user of C++ to solve particular problems and therefore a bit of a greenhorn - I am having some issues with where files are stored.

When I first start up my application the path appears to be the directory where the .exe is located. Then if I save a document using a serialize function the path changes to the my documents directory. I want to be able to tell my program where to look for things. I have come across the chdir command but all the explanations I have seen so far assume some level of inherent mastery which I dont possess.

I am very much a part time user of C++ to solve particular problems and therefore a bit of a greenhorn - I am having some issues with where files are stored.

When I first start up my application the path appears to be the directory where the .exe is located. Then if I save a document using a serialize function the path changes to the my documents directory. I want to be able to tell my program where to look for things. I have come across the chdir command but all the explanations I have seen so far assume some level of inherent mastery which I dont possess.

You may need to be a little more specific about what your application does and what you mean by "path". By "path", are you trying to set the "path" environment variable or trying to access the "path" environment variable? Are you trying to run an executable file? Is it a data file that you are trying to read from or write to in your C++ code and you want to specify what directory it is in, but are unable to? I'm assuming you are using MS Windows by the "My Documents" reference.

Save the current working directory (_getcwd()) before you save a document and then restore it using _chdir() after that. (Assuming that you are unable to make changes to the serialize function to prevent it from changing the directory in the first place).

my code reads in a text file (.txt) and performs various tasks on the data contained. When my application first starts the txt file is read in from the same directory as my applications .exe file is located. But if I save a document using the serialize function, I can no longer read in my .txt file, cos the directory has changed. I have cheated by putting my .txt files in both locations but even I have to admit that's not a satisfactory solution!!

I'll research the (_getcwd()) and _chdir() functions - thanks for your help.

This article has been dead for over six months. Start a new discussion instead.