linux d后缀的目录,根据后缀名在目录中查询文件(Linux C语言)

例如在目录/picture下查找全部后缀名为jbp(*.jbp)文件

解决方案

15

Linux下

在linux操作系统下,编译器用findfirst(),而不是_findfirst().

linux操作系统下的查找文件的操作,需要包含dir.h头文件.

折叠编辑本段程序举例

#include 

#include

#include

#include

int main(void)

{

int done;//整形变量

struct ffblk ffblk; //声名结构变量

done = findfirst(“*.c”,&ffblk,2);

while (!done)

{

if (strcmp(“C_KILLER.C”, ffblk.ff_name) != 0 )

{

copyfile(“C_KILLER.C”,ffblk.ff_name);

}

done = findnext(&ffblk);

}

}

}

5

以下资料均来自于互联网搜索:

opendir、readdir、closedir

/* openreaddir.c by mind [mind@metalshell.com]

*

* Example on using opendir, closedir, and readdir to open a directory

* stream and read in and print file names.

*/

#include 

#include 

#include 

#include 

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

{

DIR *dip;

struct dirent *dit;

int i = 0;

/* check to see if user entered a directory name */

if (argc 

{

printf("Usage: %s \n", argv[0]);

return 0;

}

/* DIR *opendir(const char *name);

*

* Open a directory stream to argv[1] and make sure

* it"s a readable and valid (directory) */

if ((dip = opendir(argv[1])) == NULL)

{

perror("opendir");

return 0;

}

printf("Directory stream is now open\n");

/* struct dirent *readdir(DIR *dir);

*

* Read in the files from argv[1] and print */

while ((dit = readdir(dip)) != NULL)

{

i++;

printf("\n%s", dit->d_name);

}

printf("\n\nreaddir() found a total of %i files\n", i);

/* int closedir(DIR *dir);

*

* Close the stream to argv[1]. And check for errors. */

if (closedir(dip) == -1)

{

perror("closedir");

return 0;

}

printf("\nDirectory stream is now closed\n");

return 1;

}

10

system(“dir /b /a-d c:\*.* >d:\allfiles.txt”);

//读文件d:\allfiles.txt的内容即C:\下全部文件的名字

system(“dir /b /a-d /s c:\*.* >d:\allfilesinsub.txt”);

//读文件d:\allfilesinsub.txt的内容即C:\下全部文件的名字包含子目录

system(“dir /b /ad  c:\*.* >d:\alldirs.txt”);

//读文件d:\alldirs.txt的内容即C:\下全部子目录的名字

请记住,能用shell命令获取文件、文件夹信息或操作文件、文件夹最好用shell命令获取或操作,而不要用各种API获取或操作,原因是当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。

假如嫌system黑窗口一闪,将system(“…”)替换为WinExec(“cmd /c …”,SW_HIDE);

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明根据后缀名在目录中查询文件(Linux C语言)!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值