用C语言如何从路径名中分离文件名

用C语言如何从路径名中分离文件名,第1张

声明一个足够长的名为fn的char型数组,调用库函数strrchr在含路径的全物大模文件名中找到文件名前的'\',将其后的文件名拷贝到fn中即可。举例代码如下:

//#include "stdafx.h"//If the vc++6.0, with this line.

#include "stdio.h"

#include "string.h"罩缓

int main(void){

    char fn[30],*p

    char pathname[80]="e:\\1\\2\\abc.dat"

     仿滑     //上句假设以某种方式获得的全文件名在pathname中,"..."中只是举例

    strcpy(fn,(p=strrchr(pathname,'\\')) ? p+1 : pathname)

          //上句函数第2实参这样写以防止文件在当前目录下时因p=NULL而出错

    printf("%s\n",fn)//打出来看看

    return 0

}

#include <迹拦stdio.h>

#include <string.h>

int main(int argc, char *argv[])

{

if(argc<3)

return -1

int n=atoi(argv[2])

int lcnt=0

char name[256]={0}

char buf[4096]={0}

FILE *srcfp=fopen(argc[1],"r")

FILE *newfp[100]={NULL}

int i

for(i=0i<ni++)

{

sprintf(name,"冲闷%s.%d"散州弯,argv[1],i+1)

newfp[i]=fopen(name,"w")

}

while(feof(srcfp)==0)

{

fgets(buf,4096,srcfp)

fputs(buf,newfp[lcnt%n])

lcnt++

}

for(i=0i<ni++)

fclose(newfp[i])

fclose(srcfp)

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12129895.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存