linux遍历下载文件,linux中遍历目录下的文件

遍历函数

#include

#include

#include

#include

#include

#include

#include

void mylist(char *filename)

{

//0.切换目录到指定的目录下

chdir(filename);

char mymode[11]="----------";

//1。打开目录

DIR *mydir=opendir("./");

if(mydir==NULL)

{

printf("opendir error!\n");

return;

}

//2。遍历目录下的文件名

struct dirent *p=NULL;

printf("------------------------------------------\n");

while( (p=readdir(mydir))!=NULL )

{

//3.把每个文件的属性读取出来

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

struct stat buf;

stat(p->d_name,&buf);

//取文件的I节点信息演示

//printf("%s %d\n",p->d_name,buf.st_ino);

//如果是设备文件,在写文件大小的地方是设备的驱动号 主驱动号/次驱动号

switch(buf.st_mode&S_IFMT)

{

case S_IFREG: mymode[0]='-'; break;//一般文件

case S_IFDIR: mymode[0]='d'; break;//目录

case S_IFCHR: mymode[0]='c'; break;//字符设备

case S_IFLNK: mymode[0]='l'; break;//链接文件

case S_IFBLK: mymode[0]='b'; break;//块设备

case S_IFSOCK:mymode[0]='s'; break;//套接口文件

case S_IFIFO: mymode[0]='p'; break;//管道文件

default:break;

}

mymode[1]=buf.st_mode&S_IRUSR?'r':'-' ;

mymode[2]=buf.st_mode&S_IWUSR?'w':'-' ;

mymode[3]=buf.st_mode&S_IXUSR?'x':'-' ;

mymode[4]=buf.st_mode&S_IRGRP?'r':'-' ;

mymode[5]=buf.st_mode&S_IWGRP?'w':'-' ;

mymode[6]=buf.st_mode&S_IXGRP?'x':'-' ;

mymode[7]=buf.st_mode&S_IROTH?'r':'-' ;

mymode[8]=buf.st_mode&S_IWOTH?'w':'-' ;

mymode[9]=buf.st_mode&S_IXOTH?'x':'-' ;

printf(&

quot;%-25s %10s\n",p->d_name,mymode);

}

}

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

{

mylist(argv[1]);

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值