C语言实现获取文件后缀、修改后缀

用于实现读取某个类型的文件,如txt,然后对文件内容进行处理之后,输出到另一个文件csv

需要根据指定的输入文件绝对路径,得到修改后缀之后的文件绝对路径,用于在本地创建。

首先是获取文件绝对路径字符串中的后缀(扩展名),实现思路是,右边第一.之后的字符串就是文件扩展名。

/*
    pFilePath:文件的绝对路径
    pOutFileExt:文件的后缀名
    pOutFileName:不含文件后缀的文件路径
    return:0-执行成功, -1-执行失败
*/
int GetFileExt(char *pFilePath, char *pOutFileExt, char *pOutFileName)
{
    char *pDest = NULL;
    int iResult = 0;
    int iFullPathLen = 0;
    int iFileExtLen  = 0;
    char szFullPathName[500] = {0};

    //参数不合法
    if ((NULL == pFilePath) || (NULL == pOutFileExt) || (NULL == pOutFileName))
    {
        printf("file is null\n");
        return -1;
    }
    printf("filePath=%s\n", pFilePath);

    //文件路径去掉\0后存入本地数组
    _snprintf(szFullPathName,sizeof(szFullPathName)-1, "%s", pFilePath);

    //文件完整路径的长度
    iFullPathLen = strlen(szFullPathName);      //12

    //右边.的位置
    pDest = strrchr(szFullPathName, '.');       //p+8
    //没有找到.
    if (NULL == pDest)
    {
        return -1;
    }

    //后缀的起始下标p+iResult
    iResult = pDest - szFullPathName + 1;       //p+8-p+1=9

    //后缀长度
    iFileExtLen = iFullPathLen - iResult;       //12-9=3,

    if (iFileExtLen > 0)
    {
        memcpy(pOutFileExt, szFullPathName+iResult, iFileExtLen);
        memcpy(pOutFileName, szFullPathName, iFullPathLen-iFileExtLen-1);
    }
    else
    {
        *pOutFileName = 0;
        *pOutFileExt = 0;
    }

    return 0;
}

然后是在不含文件后缀的文件路径字符串后面,拼接指定的后缀字符串。


/*
    pFilePath: 输入文件的绝对路径,含文件扩展名
    pOutFilePath: 修改扩展名后的文件的绝对路径
    pOutExt: 输出文件的扩展名,如"csv"

    example:

*/
int ModifyFileExt(char *pFilePath, char *pOutFilePath, const char *pOutExt)
{
    char szOutFileName[100] = {0};
    char szFileExt[100] = {0};
    char szOutFilePath[200] = {0};

    //参数不合法
    if(NULL == pFilePath || NULL == pOutFilePath || NULL == pOutExt)
        return;

    //解析失败
    if(-1 == GetFileExt(pFilePath, szFileExt, szOutFileName))
        return -1;

//    printf("path:[%s], ext:[%s], name:[%s]\n", pFilePath, szFileExt, szOutFileName);
    snprintf(szOutFilePath, sizeof(szOutFileName), "%s.%s", szOutFileName, pOutExt);

    memcpy(pOutFilePath, szOutFilePath, strlen(szOutFilePath));

    return 0;
}

实际调用:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int ModifyFileExt(char *pFilePath, char *pOutFilePath, const char *pOutExt);
int GetFileExt(char *pFilePath, char *pOutFileExt, char *pOutFileName);

int main(void)
{
	char szFilePathOut[500] = {0};
	const char szFilePathIn[] = "D:/abc.txt";
	int ret = 0;
	
	ret = ModifyFileExt(szFilePathIn, szFilePathOut, "csv");
    
    if(ret != -1)
        printf("file in:[%s], file out:[%s]\n", szFilePathIn, szFilePathOut);
	else 
        printf("filepath is null\n");
    
	return 0;
}

输出结果:

$ ./a.exe
filePath=D:/abc.txt
file in:[D:/abc.txt], file out:[D:/abc.csv]
  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

whik1194

如果对你有帮助,欢迎打赏。谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值