• C/C++获取文件后缀名并且比较


    以下这段是VC中过去文件后缀名的方法

    1。
    CString GetSuffix(CString strFileName) 

            return strFileName.Right(strFileName.GetLength()-strFileName.ReverseFind('.')-1); 
    }

    2。PathFindExtension

    3。忽略大小写方法
    CString::MakeUpper();全部转化为大写; 
    CString::MakeLower();全部转化为小写;
     
     
    取得文件的类型 PathFindExtension()

    /////////////////////////////////////////////////////////////////////////
    //
    //    PathFindExtension();函数的使用
    //    作用:取得文件的后缀名
    //   注意:头文件 shlwapi.h   包含库: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;
    }

    比较文件后缀名的方法可以用_tcscmp()函数:例如if (_tcscmp(m_pszExtension, _T(".png"))== 0 ),比较获得的文件后缀名是否为.png  

    使用C语言获取文件后缀名的技巧

     

    其实原来一直以为,C语言对于处理文件后缀名这种事是有点力不从心的(只能通过自己写一个小函数完成这种小需求),maybe C++可以有实现这种功能的STL或者类,但是因为本人对C++知之甚少,也就不在这里献丑了。

     

    今日上网本来想搜一段别人写好的代码片段,修改修改拿来用的,不过偶然在csdn论坛上有人提到了一个名叫_splitpath的c函数,貌似可以获取一个文件完整路径的各个部分,当然也包括后缀名了。既然有标准文件名处理函数,岂有不用之理。这里,小弟便为大家详解一下这个函数,以后有用到类似需求的时候,也不用自己写了(毕竟自己写的函数难免存在些bug或是漏洞不是)。

     

    _splitpath

    需要的头文件是:<stdlib.h>

    函数原型如下

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

     

    其中包含5个参数,第一个是待处理的完整的文件名路径,例如:“c:windowsmyfile.txt”,当然这个文件名也可以不是那么完整,即使是"myfile.txt"这样的字符串也可以成功处理。

    后面四个参数分别代表四个需要从原始文件路径中截取的字符串,有驱动器盘符(drive),中间的路径(dir),文件名(fname),和后缀名(ext)。

    只要在这四个参数中传入对应的字符串指针,函数返回时即可获取对应截取的字符串,不想获取的可以直接填入NULL进行忽略,比如我只想截取文件的后缀名,那么这个函数可以如下调用:

    _splitpath(path, NULL, NULL, NULL, ext);

    其中ext必须是已经分配了内存空间的字符串指针,否则会出错(c语言的基本特性,我就不赘述了)

     

    下面再附上一段完整的示例程序,供大家参考:

    Example:

    1. /* MAKEPATH.C */  
    2.   
    3. #include <stdlib.h>  
    4. #include <stdio.h>  
    5.   
    6. void main( void )  
    7. {  
    8.    char path_buffer[_MAX_PATH];  
    9.    char drive[_MAX_DRIVE];  
    10.    char dir[_MAX_DIR];  
    11.    char fname[_MAX_FNAME];  
    12.    char ext[_MAX_EXT];  
    13.   
    14.    _makepath( path_buffer, "c""\sample\crt\", "makepath", "c" );  
    15.    printf( "Path created with _makepath: %s ", path_buffer );  
    16.    _splitpath( path_buffer, drive, dir, fname, ext );  
    17.    printf( "Path extracted with _splitpath: " );  
    18.    printf( "  Drive: %s ", drive );  
    19.    printf( "  Dir: %s ", dir );  
    20.    printf( "  Filename: %s ", fname );  
    21.    printf( "  Ext: %s ", ext );  
    22. }  
    OutPut:
    1. Path created with _makepath: c:samplecrtmakepath.c  
    2.   
    3. Path extracted with _splitpath:  
    4.   Drive: c:  
    5.   Dir: samplecrt  
    6.   Filename: makepath  
    7.   Ext: .c  
  • 相关阅读:
    Qt:移动无边框窗体(使用Windows的SendMessage)
    github atom 试用
    ENode框架Conference案例转载
    技术
    NET 领域驱动设计实战系列总结
    mac 配置Python集成开发环境
    User、Role、Permission数据库设计ABP
    Oracle 树操作
    Oracle 用户权限管理方法
    Web Api 2, Oracle and Entity Framework
  • 原文地址:https://www.cnblogs.com/lisuyun/p/3399232.html
Copyright © 2020-2023  润新知