要求在Linux中遍历一个目录,并将该目录所有文件拷贝到另外一个目录中,要求“复制”时使用系统函数,“粘贴”时使用标准库函数。
时间: 2024-03-18 09:39:14 浏览: 33
在Linux中,可以使用系统函数 `opendir` 和 `readdir` 来遍历目录,使用系统函数 `stat` 来获取文件信息,使用系统函数 `open` 和 `read` 来读取文件内容,使用系统函数 `write` 和 `close` 来写入文件内容。而使用标准库函数 `fopen` 和 `fwrite` 来写入文件内容。下面是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
void copy_files(const char *src_dir, const char *dst_dir) {
DIR *dir;
struct dirent *entry;
// 打开源目录
if ((dir = opendir(src_dir)) == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
}
// 遍历目录
while ((entry = readdir(dir)) != NULL) {
char src_path[PATH_MAX];
char dst_path[PATH_MAX];
struct stat st;
// 忽略 "." 和 ".."
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
// 构造源路径和目标路径
snprintf(src_path, PATH_MAX, "%s/%s", src_dir, entry->d_name);
snprintf(dst_path, PATH_MAX, "%s/%s", dst_dir, entry->d_name);
// 获取文件信息
if (stat(src_path, &st) < 0) {
perror("stat");
exit(EXIT_FAILURE);
}
if (S_ISREG(st.st_mode)) { // 普通文件
int src_fd, dst_fd;
char buf[BUFSIZ];
ssize_t n;
// 打开源文件和目标文件
if ((src_fd = open(src_path, O_RDONLY)) < 0) {
perror("open");
exit(EXIT_FAILURE);
}
if ((dst_fd = creat(dst_path, st.st_mode)) < 0) {
perror("creat");
exit(EXIT_FAILURE);
}
// 读取源文件内容并写入目标文件
while ((n = read(src_fd, buf, BUFSIZ)) > 0) {
if (write(dst_fd, buf, n) != n) {
perror("write");
exit(EXIT_FAILURE);
}
}
// 关闭文件
close(src_fd);
close(dst_fd);
} else if (S_ISDIR(st.st_mode)) { // 目录
// 创建目标目录
if (mkdir(dst_path, st.st_mode) < 0) {
perror("mkdir");
exit(EXIT_FAILURE);
}
// 递归复制目录下的文件
copy_files(src_path, dst_path);
}
}
// 关闭目录
closedir(dir);
}
void paste_files(const char *src_dir, const char *dst_dir) {
DIR *dir;
struct dirent *entry;
// 打开源目录
if ((dir = opendir(src_dir)) == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
}
// 遍历目录
while ((entry = readdir(dir)) != NULL) {
char src_path[PATH_MAX];
char dst_path[PATH_MAX];
// 忽略 "." 和 ".."
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
// 构造源路径和目标路径
snprintf(src_path, PATH_MAX, "%s/%s", src_dir, entry->d_name);
snprintf(dst_path, PATH_MAX, "%s/%s", dst_dir, entry->d_name);
if (entry->d_type == DT_REG) { // 普通文件
FILE *src_fp, *dst_fp;
char buf[BUFSIZ];
size_t n;
// 打开源文件和目标文件
if ((src_fp = fopen(src_path, "r")) == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
if ((dst_fp = fopen(dst_path, "w")) == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
// 读取源文件内容并写入目标文件
while ((n = fread(buf, 1, BUFSIZ, src_fp)) > 0) {
if (fwrite(buf, 1, n, dst_fp) != n) {
perror("fwrite");
exit(EXIT_FAILURE);
}
}
// 关闭文件
fclose(src_fp);
fclose(dst_fp);
} else if (entry->d_type == DT_DIR) { // 目录
// 创建目标目录
if (mkdir(dst_path, 0755) < 0) {
perror("mkdir");
exit(EXIT_FAILURE);
}
// 递归粘贴目录下的文件
paste_files(src_path, dst_path);
}
}
// 关闭目录
closedir(dir);
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <src_dir> <dst_dir>\n", argv[0]);
exit(EXIT_FAILURE);
}
copy_files(argv[1], argv[2]);
paste_files(argv[2], "./paste");
return 0;
}
```
这个程序会将指定目录中的所有文件复制到另一个目录中,并在粘贴时将其复制到 `./paste` 目录中。在复制时使用了系统函数,而在粘贴时使用了标准库函数。
相关推荐
![bat](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)