c语言读取文件名称(c语言读取文件名称怎么写)

http://www.itjxue.com  2023-04-02 01:29  来源:未知  点击次数: 

c语言获取文件名

void?get_filename(char?*path,?char?*name)

{

????int?i,j?=?0;

????for(i?=?0;?path[i];?i?++)

????????if(path[i]?==?'\\')?j?=?i;

????strcpy(name,?path[j]);

}

这样得到的name就是你需要的。

PS:对于windows 路径中的是\ 而不是你题目中的/

c语言中如何读文件名字

好像有函数,但是忘了,

实在不行,你在C语言里调用DOS的dir命令写到一个文件里,然后再读那个文件。

调用DOS为:

system("dir

/ad

/b

fold.txt");

/*这个是文件夹列表*/

system("dir

/a-d

/b

file.txt");

/*这个是文件列表*/

你再读一下那两件文件。

办法是有点笨。

你自己再打开那两个文本文件看一下,里面的内容吧。

c语言读取文件名问题

用C语言读取目录中的文件名的方法:

1、如果是在window环境下,可以用一下方法:

使用stdlib.h头文件声明的system()函数

_CRTIMP int __cdecl system (const char*);

system("dir c:\\ /a:h /b c:\\dir.txt");

调用系统命令dir,把c:目录下文件列表写入文件dir.txt中

2、使用dirent.h头文件中声明的opendir(),readdir()函数;

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

{?

??DIR?*directory_pointer;?

????struct?dirent?*entry;?

???

????if((directory_pointer=opendir("d:\\XL"))==NULL)?

????????printf(?"Error?opening?\n?");?

????else?

????{?

????????while((entry=readdir(directory_pointer))!=NULL)?

????????{?

??????????printf("%s\n",entry-?d_name);

????????}?

????????closedir(directory_pointer);?

???????

????}

??system("PAUSE");

?return?0;

}

3、如果没有dirent.h,可以使用io.h头文件中声明的_findfirst(),_findnext()函数;

示例代码:

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

{

??long?file;

????struct?_finddata_t?find;

????

?????_chdir("d:\\");

?????if((file=_findfirst("*.*",?find))==-1L)

??{

??????printf("空白!\n");

?????exit(0);

?????}

?????printf("%s\n",?find.name);

?????while(_findnext(file,?find)==0)

?????{

??????????printf("%s\n",?find.name);

?????}

?????_findclose(file);

??

??system("PAUSE");

?????return?0;

}

(责任编辑:IT教学网)

更多

推荐站内动态文章