C/C++ 读取并判断文件后缀名

本文介绍了如何使用C/C++通过文件路径获取文件后缀名,并提供了`AnalyticalDecoder::decideSuffix`函数的示例,该函数结合`substr`方法从路径中提取后缀名。
摘要由CSDN通过智能技术生成

//filePath为文件路径,suffix为后缀名

bool AnalyticalDecoder::decideSuffix(  char* filePath, char* suffix )

{
    char* fileExt;
    char *ptr, c = '.';
    //最后一个出现c的位置
    ptr = strrchr(filePath, c);
    //用指针相减 求得索引
    int pos = ptr-filePath;
    //获取后缀
    fileExt=substr(filePath,pos+1,strlen(filePath));
    //判断后缀是否相同
    if (0==strcmp(fileExt,suffix))
        return true;
    else
        return false;

}

//求子串
char* AnalyticalDecoder::substr(const char*str, unsigned start, unsigned end)
{
    unsigned n = end - start;
    static char stbuf[256];
    //复制最后三个字符,即后缀
    strncpy(stbuf, str + start, n);
    //字串最后加上0
    stbuf[n] = 0;
    return stbuf;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值