zoukankan
html css js c++ java
Linux下遍历目录下的文件
方法1:
http://tag.csdn.net/Article/ef431d9b-68b3-419f-9f85-1fb9279f904a.html
//
ListFile.cpp
#include
<
stdio.h
>
#include
<
dirent.h
>
#include
<
sys
/
types.h
>
int
main(
int
argc,
char
*
argv[])
{
if
(
2
!=
argc)
{
printf(
"
Usage:ListFileSourceFolder
"
);
return
1
;
}
DIR
*
pDir
=
NULL;
struct
dirent
*
ent
=
NULL;
pDir
=
opendir(argv[
1
]);
if
(NULL
==
pDir)
{
printf(
"
Source folder not exists!
"
);
return
1
;
}
while
(NULL
!=
(ent
=
readdir(pDir)))
{
printf(
"
%s
"
, ent
->
d_name);
}
closedir(pDir);
pDir
=
NULL;
方
//
ListFile.cpp
#include
<
stdio.h
>
#include
<
dirent.h
>
#include
<
sys
/
types.h
>
int
main(
int
argc,
char
*
argv[])
{
if
(
2
!=
argc)
{
printf(
"
Usage:ListFileSourceFolder
"
);
return
1
;
}
DIR
*
pDir
=
NULL;
struct
dirent
*
ent
=
NULL;
pDir
=
opendir(argv[
1
]);
if
(NULL
==
pDir)
{
printf(
"
Source folder not exists!
"
);
return
1
;
}
while
(NULL
!=
(ent
=
readdir(pDir)))
{
printf(
"
%s
"
, ent
->
d_name);
}
closedir(pDir);
pDir
=
NULL;
法2:
http://topic.csdn.net/t/20051221/16/4472968.html
试试下面的代码,我是在redhat上编译运行的。
//
g++ -o read_dir read_dir.cpp
//
用于列出参数目录下的文件
#include
<
stdio.h
>
#include
<
stdlib.h
>
#include
<
sys
/
types.h
>
#include
<
dirent.h
>
int
main(
int
argc,
char
*
argv[])
{
DIR
*
dp;
struct
dirent
*
dirp;
if
(argc
!=
2
)
{
printf(
"
not enough arguments! exit!\n
"
);
exit(
0
);
}
if
((dp
=
opendir(argv[
1
]))
==
NULL)
{
printf(
"
can't open %s!\n
"
,argv[
1
]);
exit(
0
);
}
while
((dirp
=
readdir(dp))
!=
NULL)
printf(
"
%s\n
"
,dirp
->
d_name);
closedir(dp);
}
方法3
#include
<
stdio.h
>
#include
<
dirent.h
>
#include
<
sys
/
types.h
>
#include
<
sys
/
stat.h
>
void
dir_scan(
char
*
path,
char
*
file);
int
count
=
0
;
int
main(
int
argc,
char
*
argv[])
{
struct
stat s;
if
(argc
!=
2
)
{
printf(
"
one direction requried\n
"
);
exit(
1
);
}
if
(lstat(argv[
1
],
&
s)
<
0
)
{
printf(
"
lstat error\n
"
);
exit(
2
);
}
if
(
!
S_ISDIR(s.st_mode))
{
printf(
"
%s not direction name\n
"
,argv[
1
]);
exit(
3
);
}
dir_scan(
""
, argv[
1
]);
printf(
"
total: %d files\n
"
, count);
exit(
0
);
}
void
dir_scan(
char
*
path,cha日
*
file)
{
struct
stat s;
DIR
*
dir;
struct
dirent
*
dt;
char
dirname[
50
];
memset(dirname,
0
,
50
*
sizeof
(
char
));
strcpy(dirname,path);
if
(lstat(file,
&
s)
<
0
)
{
printf(
"
lstat error\n
"
);
}
if
(S_ISDIR(s.st_mode))
{
strcpy(dirname
+
strlen(dirname), file);
strcpy(dirname
+
strlen(dirname),
"
/
"
);
if
((dir
=
opendir(file))
==
NULL)
{
printf(
"
opendir %s/%s error\n
"
);
exit(
4
);
}
if
(chdir(file)
<
0
)
{
printf(
"
chdir error\n
"
);
exit(
5
);
}
while
((dt
=
readdir(dir))
!=
NULL)
{
if
(dt
->
d_name[
0
]
==
'
.
'
)
{
continue
;
}
dir_scan(dirname,dt
->
d_name);
}
if
(chdir(
"
..
"
)
<
0
)
{
printf(
"
chdir error\n
"
);
exit(
6
);
}
}
else
{
printf(
"
%s%s\n
"
, dirname, file);
count
++
;
}
}
查看全文
相关阅读:
python_元组
python_列表
python_字符串
python_序列
RFS一些基本概念
学习RFS,所有文章的参考
HDU 1754 线段树 单点跟新 HDU 1166 敌兵布阵 线段树 区间求和
第四届河南省ACM SUBSTRING 字符串处理
蓝桥杯 手链样式 排列组合
蓝桥杯 牌型种数 DFS
原文地址:https://www.cnblogs.com/cy163/p/730294.html
最新文章
Java发送http请求发送json对象
Mybatis缓存+配置
架构设计之设计模式 (一) 适配器(Adapter)---提高复用性
架构设计之设计模式 (四) Java中多种方式实现单例模式
J2EE (十) 简洁的JSTL、EL
架构设计之设计模式 (二) 静态代理和动态代理--间接“美”
架构设计之设计模式 (三) 策略模式+装饰模式+反射(java)
J2EE (七) 配置文件详解以及dom4j进行解析
J2EE (六) 详解 java 中文乱码
.NET 中合理使用连接池
热门文章
iptables
mysql事件的开启和调用
为什么MyISAM会比Innodb的查询速度快
数据库SQL SELECT查询的工作原理
MySql按周,按月,按日分组统计数据
PHP简单利用 token 防止表单重复提交
设计模式学习(七)、命令模式
设计模式学习(六)、单例模式
更新teaching中fdSubjectID为null的老数据
设计模式学习(五)、抽象工厂模式
Copyright © 2011-2022 走看看