In practical applications, the C ++ programming language helps developers easily implement various functions and requirements. For example, you can flexibly use various methods to perform operations on files. Next, we will introduce how C ++ obtains the current path.
Example code for C ++ to obtain the current path:
1. the following code is from csdn 2004 VC programming experience.
- {
- CString strPath;
- GetCurrentDirectory(MAX_PATH,strPath.GetBuffer(MAX_PATH));
- strPath.ReleaseBuffer();
- return strPath;
- }
2. The following code is from tz mfc. Net primer 1.01,
- CString CPropertyGridSampleApp::GetCurrWorkingDir()
- {
- CString strPath;
- TCHAR szFull[_MAX_PATH];
- TCHAR szDrive[_MAX_DRIVE];
- TCHAR szDir[_MAX_DIR];
- ::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR));
- _tsplitpath(szFull, szDrive, szDir, NULL, NULL);
- _tcscpy(szFull, szDrive);
- _tcscat(szFull, szDir);
- strPath = CString(szFull);
- return strPath;
- }
Use section C ++ to obtain the code of the current path to obtain the directory where the application is running. However, because Microsoft Visual Studio. when compiling and running a project, the real host is IDE, so the current directory is the directory where the project is located, not the debug or release directory. This requires attention from developers. The second code can solve this problem dynamically. Unicode support (tchar) is added ).