C语言小程序

实现一个函数,取出一个全路径文件名中的文件名部分,函数原型如下:

/*  [in] pszfullpath  全路径文件名

     [out] pszpathname  接受全路径的缓冲区

     [out]  ncount    缓冲区的大小

*/

方法一: 常规方法

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int ExtractFilePath(char *pszfullpath,char *pszpathname,int ncount)
{
   char kong[20]={0};
   int num,i;
   pszpathname=NULL;
   printf("请输入文件全路径!\n");
   pszfullpath = (char *)malloc(30*sizeof(char));
   scanf("%s",pszfullpath);
   
   num=strlen(pszfullpath);   //计算全路径的长度
   printf("%d\n",num);
   ncount = 0;
   for(i=0;i<num;i++)
   {  
 if(pszfullpath[i] == '/')   //如果遇到'/',则将ncount 赋值为零,即清空之前所存储的字符
 {
 printf("aaa\n");
     ncount = 0;
 }
 else                           //否则就ncount ++
 {  
ncount++;
printf("ttt\n");
 }
   }
   pszpathname = (char *)malloc(21*sizeof(char));
   strcpy(pszpathname,pszfullpath+num-ncount);
   printf("qqq\n");
   if (pszpathname)    //输出文件名
{
  printf(" %s\n",pszpathname);
    printf("the length is %d\n",ncount);
}
    else
        printf("The character was not found\n");
    return 0;
}
int main()
{
char *pszfullpath1,*pszpathname1;
int ncount1;
ExtractFilePath(pszfullpath1,pszpathname1,ncount1);
return 0;
}

方法二:  strrchr  函数的应用

#include <string.h>
#include <stdio.h>


int ExtractFilePath(char *pszfullpath,char *pszpathname,int ncount)
{
char string[20];
char c = '/';
pszfullpath=string;
printf("请输入文件全路径!\n");
scanf("%s",string);
pszpathname = strrchr(pszfullpath, c);
ncount = strlen(pszpathname+1);
printf("the length is : %d\n",ncount);
if (pszpathname)
{
  printf("The character %c is at position: %s\n", c, pszpathname+1);
    printf("the length is %d\n",ncount);
}
    else
        printf("The character was not found\n");
    return 0;
}
int main()
{
char *pszfullpath1,*pszpathname1;
int ncount1;
ExtractFilePath(pszfullpath1,pszpathname1,ncount1);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zxa1334

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值