How to use C ++ to obtain the current path

Source: Internet
Author: User

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.

 
 
  1. {   
  2. CString strPath;   
  3. GetCurrentDirectory(MAX_PATH,strPath.GetBuffer(MAX_PATH));   
  4. strPath.ReleaseBuffer();   
  5. return strPath;   

2. The following code is from tz mfc. Net primer 1.01,

 
 
  1. CString CPropertyGridSampleApp::GetCurrWorkingDir()   
  2. {   
  3. CString strPath;   
  4. TCHAR szFull[_MAX_PATH];   
  5. TCHAR szDrive[_MAX_DRIVE];   
  6. TCHAR szDir[_MAX_DIR];   
  7. ::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR));   
  8. _tsplitpath(szFull, szDrive, szDir, NULL, NULL);   
  9. _tcscpy(szFull, szDrive);   
  10. _tcscat(szFull, szDir);   
  11. strPath = CString(szFull);   
  12. 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 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.