"Go" MFC string processing

Source: Internet
Author: User

Original URL: http://www.cnblogs.com/lisuyun/p/3399232.html

C + + Gets the file suffix name and compares the following this paragraph is the VC in the past file suffix Method 1.
CString Getsuffix (CString strFileName)
{
Return Strfilename.right (Strfilename.getlength ()-strfilename.reversefind ('. ') -1);
}

2. PathFindExtension

3. Ignore Case method
Cstring::makeupper (); convert all to uppercase;
Cstring::makelower (); convert all to lowercase; Gets the type of file PathFindExtension ()

/////////////////////////////////////////////////////////////////////////
//
PathFindExtension (); Use of functions
Function: Obtain the suffix name of the file
Note: The header file shlwapi.h contains the library: Shlwapi.lib
//
//
/////////////////////////////////////////////////////////////////////////

#include <tchar.h>
#include <iostream>
#include <Windows.h>
#include <shlwapi.h>
using namespace Std;

#pragma comment (lib, "Shlwapi.lib")

int _tmain (int argc, _tchar* argv[])
{
TCHAR szexepath[2048];
LPTSTR pszextension;
GetModuleFileName (null,szexepath,2048);
Pszextension = PathFindExtension (Szexepath);

return 0;
}

The method for comparing file suffix names can be used with the _tcscmp () function: for example if (_tcscmp (M_pszextension, _t (". png")) = = 0), comparing the obtained file suffix with the. png name.

tips for using the C language to get file suffix names

Language Cpathbufferextnull

In fact, always thought, C language for processing file suffix This kind of thing is a bit out of reach (only by writing a small function to complete this small demand), maybe C + + can have the implementation of this function STL or class, but because I know little about C + +, it is not here caught dead.

Today on the Internet would like to search for someone else to write a code snippet, modify the changes to use, but occasionally in the CSDN forum someone mentioned a C function called _splitpath, seemingly can get a file full path of the various parts, of course, including the suffix name. Since there is a standard file name processing function, there is no reason. Here, the younger brother for everyone to explain this function, later useful to similar needs, do not have to write their own (after all, the function of their own inevitably there are some bugs or loopholes are not).

_splitpath

The required header file is:<stdlib.h>

Function prototypes are as follows

void _splitpath (const char *path, char *Drive, char *dir, char *fname, char *ext);

It contains 5 parameters, the first is the complete file name path to be processed, for example: "C:\windows\myfile.txt", of course, the file name may not be so complete, even if the "myfile.txt" such a string can be successfully processed.

The following four parameters represent four strings that need to be intercepted from the original file path, with drive letter (drives), intermediate path (dir), file name (fname), and suffix (ext).

As long as the corresponding string pointer is passed in these four parameters, the function returns to get the corresponding truncated string, do not want to get directly filled with NULL to ignore, such as I just want to intercept the file suffix, then this function can be called as follows:

_splitpath (path, NULL, NULL, NULL, EXT);

Where ext must be a string pointer that has been allocated memory space, otherwise it will go wrong (basic features of C language, I will not repeat it)

Here is a complete sample program for your reference:

Example:

  1. /* Makepath. C */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. void Main ( void)
  5. {
  6. Char Path_buffer[_max_path];
  7. Char drive[_max_drive];
  8. Char Dir[_max_dir];
  9. Char Fname[_max_fname];
  10. Char Ext[_max_ext];
  11. _makepath (Path_buffer, "C", "\\sample\\crt\\", "Makepath", "C");
  12. printf ( "Path created with _makepath:%s\n\n", Path_buffer);
  13. _splitpath (Path_buffer, Drive, dir, fname, ext);
  14. printf ( "Path extracted with _splitpath:\n");
  15. printf ( "Drive:%s\n", drive);
  16. printf ( "dir:%s\n", dir);
  17. printf ( "Filename:%s\n", fname);
  18. printf ( "ext:%s\n", ext);
  19. }
OutPut:
    1. Path created with _MAKEPATH:C:\SAMPLE\CRT\MAKEPATH.C
    2. Path extracted with _splitpath:
    3. DRIVE:C:
    4. Dir: \sample\crt\
    5. Filename:makepath
    6. EXT:. C

"Go" MFC string processing

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.