Linux下关于目录的操作及其程序设计

摘要:此为Linux下目录的操作,通过学习目录操作使用C语言模拟读取目录信息等,进一步进行模拟程序的编写。

读取目录

  1. 打开目录
  2. 读取目录信息
  3. 关闭目录

打开目录

头文件dirent.h

DIR* opendir(目录名称);

成功: 地址

失败: NULL

关闭目录

closedir(DIR* 指针);

成功: 0

失败: -1

读取目录信息

struct dirent* readdir(DIR* 指针);

成功: 返回结构体指针

失败: NULL

方式: 循环自动读取

结构体内容:

​ dt_tpye:目录类型

​ dt_name:名称

tips:

如果文件名以“.”引导,该文件不可见

.为当前目录

..为父目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
int bail(const char* str){
printf("Error(%d):",errno);
perror(str);
exit(-1);
}

int main(int argc, char** argv)
{
DIR* dp = NULL;
struct dirent* dir = NULL;

dp = opendir(argv[1]);
if (dp == NULL)
bail("open dir error.");

while((dir = readdir(dp))!= NULL)
{
if(dir ->d_name[0] =='.')
continue;

if(dir -> d_type == DT_REG)
printf("Regular file: %s \n",dir->d_name);

if(dir -> d_type == DT_DIR)
printf("Dir file: %s\n",dir->d_name);
}
closedir(dp);
return 0;
}

创建目录

在Linux中可以使用命令 mkdir 进行创建目录,也可以在程序设计中使用mkdir进行创建。

创建目录函数

mkdir (目录名称,权限参数)

成功返回: 0

失败返回: -1

切换目录函数

chdir(新目录名称)

成功返回: 0

失败返回: -1

例如:

  1. 给定源目录
  2. 遍历目录
  3. 创建一个新目录
  4. 将源目录加入到新目录中

假定:

  1. 一级目录,不进入子目录
  2. 只创建,不复制

可行性:

cpdir( source,dest)中

  1. source 要存在,可读
  2. dest 要不存在,可写
  3. 读取source状态,作为dest状态
  4. 创建目标目录
  5. source: readdir;chdir(dest)
  6. dest: mkdir/create;chdir(source)
  7. 关闭两个目录指针
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#define LEN 512
int bail(const char* str)
{
printf("Error(%d):",errno);
perror(str);
exit(-1);
}

int main(int argc, char** argv)
{
char sdir[1024]={0};
char ddir[1024]={0};
char sname[1024] = {0};
char dname[1024] = {0};
int ret = -1;
int create_fd = -1;
struct dirent* dir = NULL;
DIR* dp = NULL;
struct stat sb;

if(access(argv[1],F_OK) < 0) //判断源文件是否存在,不存在则报错
bail("Source directory not exist!");
if(access(argv[2],F_OK) == 0) //判断目标文件是否存在,存在则报错
bail("Dest directory exist!");
getcwd(sdir,sizeof(sdir)); //获取源文件路径,存入sdir
memset(&sb,0,sizeof(sb)); //清空sb
stat(argv[1],&sb); // 获取权限存入sb块中

if((mkdir(argv[2],sb.st_mode)) < 0) //创建目标目录,并判断是否创建成功
bail("Create dest directory error!");
chdir(argv[2]); //进入目标路径
getcwd(ddir,sizeof(ddir)); //获取目标路径,存入ddir
chdir(sdir); //回到源目录进行读写操作

dp = opendir(argv[1]);
while((dir = readdir(dp)) != NULL)
{
//假定只有普通文件和目录文件
if (dir->d_name[0] == '.') //如果读取文件第一位为“.”,则跳过
continue;
else if (dir->d_type == DT_DIR) //如果读取类型是文件夹
{
memset(&sb,0,sizeof(sb));
stat(dir->d_name ,&sb);
chdir(ddir);
mkdir(dir->d_name,sb.st_mode);
chdir(sdir);
}
else
{
memset(&sb,0,sizeof(sb));
stat(dir->d_name,&sb);
chdir(ddir);
create_fd = creat(dir->d_name,sb.st_mode);
// 写入内容,调用复制,自行填写
close(create_fd);
chdir(sdir);
}
}

closedir(dp);

}
------- 本文结束  感谢您的阅读 -------