VC++6.0 relative path invalid solution

 

When we develop programs, we often need to manipulate related files. There are generally two ways to manipulate files: absolute paths and relative paths . Absolute paths start from the drive letter, and relative paths are relative to the current directory.

Absolute paths are very simple and generally do not go wrong, but absolute paths should be used with caution in the actual development process , because this will lead to poor program portability. But personally I have encountered several situations where relative paths do not work, and only when they are replaced with absolute paths will they take effect. I personally debug for a long time to find the reason.

There are only two cases where relative paths don't work:

  1. The writing of the relative path is wrong.
  2. The system default current directory has been changed.

Some people may have questions about the second case. Can the default current directory of the system be changed?

There are many reasons that cause the default current directory of the system to be changed. For example, on the Windows operating system, when a file is successfully opened with the file dialog box, the current directory is changed to the directory where the opened file is located. (VC++6.0 has been verified, other versions are unknown) I personally feel that this is a bug, and I hope Microsoft can solve it. There are also environment variables that may cause the default current directory of the system to be changed.

Windows systems can use the GetCurrentDirectory() function to get the current directory.

 

In fact, there is a compromise solution that can solve the above method. Obtain the default current directory of the system through the function , and then add the relative path . This writing method is very safe.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324811947&siteId=291194637