Linux下遍历目录下的文件 2
来源:互联网 发布:电脑游戏加速软件 编辑:程序博客网 时间:2024/06/05 02:52
Linux下遍历目录下的文件
方法1:
http://tag.csdn.net/Article/ef431d9b-68b3-419f-9f85-1fb9279f904a.html
方
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;
方法3
#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;
方法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++;
}
}
#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++;
}
}
方
//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
#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);
}
//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);
}
- Linux下遍历目录下的文件 2
- linux遍历目录下的文件
- LINUX下目录文件的遍历---编程
- linux 遍历目录下的文件
- linux 下遍历目录下的所有文件
- Linux下遍历目录下的所有文件
- C++实现Linux下遍历指定目录下的文件
- 遍历目录下的文件
- 遍历目录下的文件
- 遍历目录下的文件
- 遍历目录,获取目录下的文件
- 自己写的一个LINUX 下目录文件遍历程序
- Linux C :遍历输出指定目录下的所有文件
- Linux C :遍历输出指定目录下的所有文件
- Linux C :遍历输出指定目录下的所有文件
- Linux C :遍历输出指定目录下的所有文件
- 递归遍历Linux下的目录文件源码实现
- Linux C :遍历输出指定目录下的所有文件
- 2008年春季IDF 中国上海获得首发权
- linux下目录操作记录1
- 常用正则表达式
- PHP生成静态html网页的三种方法
- 利用COM+,来提高ASP执行权限的问
- Linux下遍历目录下的文件 2
- 深入研究Windows内部原理系列 http://www.microsoft.com/china/technet/webcasts/class/windowsserver1.mspx
- 如何制作嵌入式Linux文件系统
- Find Error--Print Customer Order Invoices
- 怎么获得主窗口的CWnd*?怎么把CWnd*变成HWND?怎么给HWND发送WM_USER消息?SendMessage(hwnd,WM_USER,0,0);可是主窗口接收不到
- MBone
- 北京用友面试经历
- Microsoft为什么错过了Internet
- 《深入研究Windows内部原理系列课程》[ISO]http://lib.verycd.com/2007/04/12/0000146419.html