Changing relative path reference

FILE* pFile = fopen ("./myfile.txt","w+");

Is there a way to change what a relative path is referencing after runtime has started?

My specific issue is that I have several existing 100,000+ LOC lua projects. I am building a new back-end in C/C++ for this by wrapping a Lua C library. The lua scripts use io.open("./path/relative/to/project/file") which translates to fopen(...) in C. The project file which this is relative to is opened by the user after the application has started.

I see two solutions, neither are particularly good:
1) Change every io.open() function in every project to be relative to a static location. I lose all flexibility if I do this.
2) Modify the Lua library to prepend a custom path to opened files. Modifying 3rd party libraries suck because you can't update the library without tracking patches.
3?) Ask cplusplus forum if there is a way to change the relative directory.
Yes, both Windows and *nix provide API functions to change the current working directory.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa363806(v=vs.85).aspx
http://man7.org/linux/man-pages/man2/chdir.2.html

I think you can also control the working directory of newly created processes too.
Last edited on
That's perfect, thanks! And also appreciated for showing two ways. I'll use them both:
1
2
3
4
5
6
7
8
void Project::ChangeWorkingDir()
{
#if defined(WIN32)
    SetCurrentDirectory( m_projPath.c_str() );
#elif defined(UNIX)
    chdir( m_projPath.c_str() );
#endif
}

There is probably a platform-independent way via Boost, if you are willing to add the dependency.
You're right, that's even better. I already used boost::filesystem in that source file for iterating through files. I tried it and it works as advertised.
http://www.boost.org/doc/libs/1_56_0/libs/filesystem/doc/reference.html#current_path
1
2
3
4
void Project::ChangeWorkingDir()
{
    boost::filesystem::current_path( m_projPath );
}
Last edited on
Topic archived. No new replies allowed.