linux 获取文件名函数,linux c实现的提取文件名的小程序

[email protected]

etangyushan

*工作中很多时候会和文件名打交道,有时候只需要文件名称,就写了这么一个小程序

*这个函数实现了把一个文件的绝对路径和后缀去除,只留下文件名的功能

*

*/

#include

#include

#include

//找到最后的slash(/)

int last_mark (char *str, char mark)

{

int

site = 0;

int count = 0;

while (site <=

strlen(str))

{

if (str[site++] ==

mark)

{

count =

site;

}

}

return count;

}

//找到第一个dot(.)

int first_mark (char *str, char mark, int

num)

{

int count = num;

while

(1)

{

if (str[count++] ==

mark)

{

break;

}

}

return

count;

}

/* 从文件全名中把文件名提取出来,没有后缀 */

int substr (char *srcstr, char **decstr, int lastslash, int

firstdot)

{

int i = 0;

//int ls = lastslash;

char

*str = *decstr;

printf("last=%d,first=%d\n", lastslash,

firstdot);

printf("size=%d\n", firstdot-lastslash);

int size =

firstdot-lastslash-1;

for (i=0; i

i++)

{

//str[i] =

srcstr[ls++];

printf("...%c...\n",srcstr[lastslash]);

str[i]

= srcstr[lastslash++];

}

}

//测试

int main()

{

char *file =

"/root/etc/init.d/mytettttt.c";

char *name =

(char*)malloc(256);

int lastnum = last_mark (file,

‘/‘);

//printf ("lastnum=%d\n", lastnum);

int firstnum =

first_mark (file, ‘.‘, lastnum);

//printf ("firstnum=%d\n",

firstnum);

substr (file, &name, lastnum, firstnum);

printf

("name = %s\n", name);

free(name);

name = NULL;

}

原文:http://www.cnblogs.com/etangyushan/p/3709954.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值