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.