Detailed explanation of path Decomposition Based on Windows APIs

Source: Internet
Author: User

In many codes, many programmers use the string lookup function in the c library to obtain the file name or extension of a path string. This is a lot of code, and it is also troublesome.
In fact, it can be obtained in a better way in VC. The Code is as follows: Copy codeThe Code is as follows: LPTSTR szfileName = new TCHAR [MAX_PATH];

GetModuleFileName (NULL, szfileName, MAX_PATH );

// Obtain the drive letter, file name, extension, and other information
Wchar_t drive [_ MAX_DRIVE];
Wchar_t dir [_ MAX_DIR];
Wchar_t fname [_ MAX_FNAME];
Wchar_t ext [_ MAX_EXT];
Errno_t err;
Err =: _ wsplitpath_s (szFileName, drive, _ MAX_DRIVE, dir, _ MAX_DIR,
Fname, _ MAX_FNAME, ext, _ MAX_EXT );
// Only obtain the path
PathRemoveFileSpec (szFileName );

Note:The _ wsplitpath_s function does not change the content in szFileName, but PathRemoveFileSpec does.
Header file :_Wsplitpath_s () is in stdlib. h.
The PathRemoveFileSpec () function is in shlwapi. h.

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.