
声明一个足够长的名为fn的char型数组,调用库函数strrchr在含路径的全文件名中找到文件名前的'\',将其后的文件名拷贝到fn中即可。举例代码如下:
//#include "stdafxh"//If the vc++60, with this line
#include "stdioh"
#include "stringh"
int main(void){
char fn[30],p;
char pathname[80]="e:\\1\\2\\abcdat";
//上句假设以某种方式获得的全文件名在pathname中,""中只是举例
strcpy(fn,(p=strrchr(pathname,'\\')) p+1 : pathname);
//上句函数第2实参这样写以防止文件在当前目录下时因p=NULL而出错
printf("%s\n",fn);//打出来看看
return 0;
}
C#获取文件夹名和文件个数,参考代码如下:
DirectoryInfo di = new DirectoryInfo(@"文件夹路径");diGetDirectories();//获取子文件夹列表
diGetFiles();//获取文件列表
如果嵌套文件夹个数较多,可以使用子线程统计,参考代码如下:
int count = 0;public void GetFilesCount(string path)
{
//开子线程去统计
count += SystemIODirectoryGetFiles(path)Length;
foreach (var folder in SystemIODirectoryGetDirectories(path))
{
count += GetFilesCount(folder);
}
}
用system
调用
DOS
DIR
命令就可以了:
system
(
"dir
sss_
/B
>
logtxt");
这就把
前缀为sss_的文抄件
文件名
存入
logtxt
文件了。
一个名字袭一行,没有别的东西。
你再
读出zhidao来。
#include
<stdioh>
main()
{
FILE
fp;
char
str[30][50];
//
假定文件数不超过30个
int
i,n=0;
system("dir
sss_
/B
>
logtxt");
fp=fopen("logtxt","r");
while(1){
if
(
fgets(str[n],50,fp)==NULL)
break;
str[n][strlen(str[n])-1]='\0';
//
加一个字符串结束符
n++;
}
fclose(fp);
for
(i=0;i<n;i++)
printf("%s\n",str[i]);
}
以上就是关于怎样用C编程从路径中分离出一个文件名全部的内容,包括:怎样用C编程从路径中分离出一个文件名、C#获取文件夹名和文件个数、c语言 读取目录中的文件名,并将其存入数组中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)