linux文件夹操作及递归遍历文件夹

文件夹相关函数介绍

//mkdir 函数创建文件夹

#include <sys/stat.h>
#include <sys/types.h>

int mkdir(const char *pathname, mode_t mode);

//rmdir 删除文件夹

#include <unistd.h>
int rmdir(const char *pathname);

//dopendir/fdopendir  //打开文件夹

DIR是一个结构体,是一个内部结构,用来存储读取文件夹的相关信息。

DIR *opendir(const char *name);
DIR *fdopendir(int fd);

//readdir 读文件夹

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

struct dirent {
    ino_t d_ino; /* inode number */
    off_t d_off; /* offset to the next dirent */
    unsigned short d_reclen; /* length of this record */
    unsigned char d_type; /* type of file; not supportedby all file system types */
    char d_name[256]; /* filename */
};

readdir 每次返回一条记录项。。DIR*指针指向下一条记录项。

//rewinddir

#include <sys/types.h>
#include <dirent.h>

void rewinddir(DIR *dirp);

把文件夹指针恢复到文件夹的起始位置。

//telldir函数

 #include <dirent.h>

 long telldir(DIR *dirp);

函数返回值是为文件夹流的当前位置,表示文件夹文件距开头的偏移量。

//seekdir

#include <dirent.h>
void seekdir(DIR *dirp, long offset);

seekdir表示设置文件流指针位置。

//closedir 关闭文件夹流

 #include <sys/types.h>
 #include <dirent.h>

 int closedir(DIR *dirp);

使用递归来遍历文件夹下的文件

#include<stdio.h>
#include <errno.h>
#include<stdlib.h>
#include<string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define MAX_PATH 512

void print_file_info(char *pathname);
void dir_order(char *pathname);

void dir_order(char *pathname)
{
	DIR *dfd;
	char name[MAX_PATH];
	struct dirent *dp;
	if ((dfd = opendir(pathname)) == NULL)
	{
		printf("dir_order: can‘t open %s\n %s", pathname,strerror(errno));
		return;
	}
	while ((dp = readdir(dfd)) != NULL)
	{
		if (strncmp(dp->d_name, ".", 1) == 0)
			continue; /* 跳过当前文件夹和上一层文件夹以及隐藏文件*/
		if (strlen(pathname) + strlen(dp->d_name) + 2 > sizeof(name))
		{
			printf("dir_order: name %s %s too long\n", pathname, dp->d_name);
		} else
		{
			memset(name, 0, sizeof(name));
			sprintf(name, "%s/%s", pathname, dp->d_name);
			print_file_info(name);
		}
	}
	closedir(dfd);

}
void print_file_info(char *pathname)
{
	struct stat filestat;
	if (stat(pathname, &filestat) == -1)
	{
		printf("cannot access the file %s", pathname);
		return;
	}
	if ((filestat.st_mode & S_IFMT) == S_IFDIR)
	{
		dir_order(pathname);
	}
	printf("%s %8ld\n", pathname, filestat.st_size);
}
int main(int argc, char *argv[])
{
	if (argc == 1)
	{
		dir_order(".");
	} else
	{
		dir_order(argv[1]);
	}
	return 0;
}
时间: 2024-08-06 05:38:04

linux文件夹操作及递归遍历文件夹的相关文章

c# 封装的文件夹操作类之复制文件夹

c#  封装的文件夹操作类之复制文件夹 一.复制文件夹原理: 1.递归遍历文件夹 2.复制文件 二.FolderHelper.cs 1 /// <summary> 2 /// 文件夹操作类 3 /// </summary> 4 public static class FolderHelper 5 { 6 /// <summary> 7 /// 复制文件夹 8 /// </summary> 9 /// <param name="sourceFo

Linux下的C程序,遍历文件夹并统计其中各个类型文件所占百分比

递归遍历一个目录下的所有文件和文件夹,统计各个类型文件所占的百分比 程序代码a.cpp(编译命令:g++ a.cpp -o a) #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <dirent.h> #include <string.h> stru

[C#]递归遍历文件夹

// <summary> /// 递归获取文件夹目录下文件 /// </summary> /// <param name="pathName">需要递归遍历的文件夹</param> /// <param name="fileRule">遍历规则『委托』</param> public static void LoopFolder(string pathName, Action<FileInf

java File基本操作,以及递归遍历文件夹

java 的文件操作,相对来说是比较重要的,无论是编写CS还是BS程序,都避免不了要与文件打交道,例如读写配置文件等.虽然现在很多框架都直接帮你做好了这一步! java.io.File 底层是调用与c语言接的接口,所以我们暂时不需要知道底层是怎么实现的,再说了,也看不见,最多就是看见一个接口而已.我们只需要知道java.io.File提供给我们对文件的一些操作就行了. 1.文件的创建 :java的文件创建可以直接通过new一个对象来实现. File file = new File(String

Java File 类 文件的常用操作(递归遍历所有文件,统计文件大小,删除文件)

1 package com.beiwo.homework; 2 3 import java.io.File; 4 5 /* 6 4.将一个文件夹中所有的文件查找出来,按照一定的格式 7 分析 : 1.一个文件夹中存在文件,子文件夹 8 2.除了当前文件夹,还有子文件夹中的文件也需要找出来. 9 3.子文件查找文件和当前文件查找文件是一样的操作,就可以定义一个方法来做文件查找 10 11 3.删除一周前的数据 12 13 2.获取文件夹中所有文件的大小 14 15 1.判断是否有隐藏文件 16 1

递归遍历文件夹、取所有文件。包括子目录。

private int count = 0; private int TransformFiles(string path) { DirectoryInfo dir = new DirectoryInfo(path); DirectoryInfo[] dirs = dir.GetDirectories(); //获取子目录 FileInfo[] files = dir.GetFiles("*.*"); //获取文件名 List<string> list = new List

XML DTD约束 对xml文件的crud的查询Read Retrieve操作 xml递归遍历

本地的dtd文档 xml中引入dtd文档 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE 书架 SYSTEM "book.dtd"> <书架> <书> <书名>Java就业培训教程</书名> <作者>张孝祥</作者> <售价>39.00</售价> </书> <书

C#追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件

C#追加文件 StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt"); sw.WriteLine("追逐理想"); sw.WriteLine("kzlll"); sw.WriteLine(".NET笔记"); sw.Flush(); sw.Close();C#拷贝文件 string OrignFile,NewFile; Or

Java解压上传zip或rar文件,并解压遍历文件中的html的路径

1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception { HttpSession session = request.getSession(); User user = (User) session.getAttribute(Constant.USER_SESSION_KEY); String realName = user.getRealName(); System.out.println("--------获取登录用户信