C语言打造超实用资源管理器:文件管理、预览与操作全实现!
# C语言资源管理器实现方案 下面是一个基于C语言的控制台资源管理器实现方案,使用标准库函数完成文件和目录操作。 ## 核心代码实现 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <sys/stat.h> #include <unistd.h> #include <errno.h> #define MAX_PATH 1024 #define MAX_FILENAME 256 // 显示目录内容 void list_directory(const char *path) { DIR *dir; struct dirent *entry; struct stat file_stat; printf("\n目录内容: %s\n", path); printf("--------------------------------\n"); if ((dir = opendir(path)) == NULL) { perror("无法打开目录"); return; } while ((entry = readdir(dir)) != NULL) { char full_path[MAX_PATH]; snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name); if (stat(full_path, &file_stat) == -1) { perror("stat"); continue; } printf("%c %10ld %s\n", S_ISDIR(file_stat.st_mode) ? 'D' : 'F', file_stat.st_size, entry->d_name); } closedir(dir); printf("--------------------------------\n"); } // 创建目录 void create_directory(const char *path) { if (mkdir(path, 0755) == -1) { perror("创建目录失败"); } else { printf("目录创建成功: %s\n", path); } } // 删除文件或空目录 void delete_item(const char *path) { struct stat file_stat; if (stat(path, &file_stat) == -1) { perror("stat"); return; } if (S_ISDIR(file_stat.st_mode)) { if (rmdir(path) == -1) { perror("删除目录失败"); } else { printf("目录删除成功: %s\n", path); } } else { if (unlink(path) == -1) { perror("删除文件失败"); } else { printf("文件删除成功: %s\n", path); } } } // 复制文件 void copy_file(const char *src, const char *dest) { FILE *src_file, *dest_file; char buffer[4096]; size_t bytes; src_file = fopen(src, "rb"); if (!src_file) { perror("无法打开源文件"); return; } dest_file = fopen(dest, "wb"); if (!dest_file) { perror("无法创建目标文件"); fclose(src_file); return; } while ((bytes = fread(buffer, 1, sizeof(buffer), src_file)) > 0) { fwrite(buffer, 1, bytes, dest_file); } fclose(src_file); fclose(dest_file); printf("文件复制成功: %s -> %s\n", src, dest); } // 移动/重命名文件或目录 void move_item(const char *src, const char *dest) { if (rename(src, dest) == -1) { perror("移动失败"); } else { printf("移动成功: %s -> %s\n", src, dest); } } // 显示树状目录结构 void show_tree(const char *path, int level) { DIR *dir; struct dirent *entry; struct stat file_stat; if ((dir = opendir(path)) == NULL) { return; } while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } char full_path[MAX_PATH]; snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name); if (stat(full_path, &file_stat) == -1) { continue; } for (int i = 0; i < level; i++) { printf(" "); } printf("|-- %s\n", entry->d_name); if (S_ISDIR(file_stat.st_mode)) { show_tree(full_path, level + 1); } } closedir(dir); } // 主菜单 void display_menu() { printf("\n资源管理器菜单\n"); printf("1. 查看当前目录内容\n"); printf("2. 创建目录\n"); printf("3. 删除文件/目录\n"); printf("4. 复制文件\n"); printf("5. 移动/重命名文件/目录\n"); printf("6. 显示目录树\n"); printf("0. 退出\n"); printf("请选择操作: "); } int main() { char current_dir[MAX_PATH] = "."; char path1[MAX_PATH], path2[MAX_PATH]; int choice; while (1) { display_menu(); scanf("%d", &choice); getchar(); // 消耗换行符 switch (choice) { case 1: list_directory(current_dir); break; case 2: printf("输入要创建的目录名: "); fgets(path1, sizeof(path1), stdin); path1[strcspn(path1, "\n")] = '\0'; create_directory(path1); break; case 3: printf("输入要删除的文件/目录名: "); fgets(path1, sizeof(path1), stdin); path1[strcspn(path1, "\n")] = '\0'; delete_item(path1); break; case 4: printf("输入源文件名: "); fgets(path1, sizeof(path1), stdin); path1[strcspn(path1, "\n")] = '\0'; printf("输入目标文件名: "); fgets(path2, sizeof(path2), stdin); path2[strcspn(path2, "\n")] = '\0'; copy_file(path1, path2); break; case 5: printf("输入源文件/目录名: "); fgets(path1, sizeof(path1), stdin); path1[strcspn(path1, "\n")] = '\0'; printf("输入目标文件/目录名: "); fgets(path2, sizeof(path2), stdin); path2[strcspn(path2, "\n")] = '\0'; move_item(path1, path2); break; case 6: printf("\n目录树结构:\n"); show_tree(current_dir, 0); break; case 0: printf("退出资源管理器\n"); exit(0); default: printf("无效选择,请重试\n"); } } return 0; } ``` ## 代码说明 1. 目录列表功能:`list_directory()`函数使用`opendir()`和`readdir()`遍历目录内容,显示文件和子目录。 2. 目录创建:`create_directory()`使用`mkdir()`系统调用创建新目录。 3. 删除功能:`delete_item()`区分文件和目录,分别使用`unlink()`和`rmdir()`。 4. 文件复制:`copy_file()`通过二进制读写实现文件内容复制。 5. 移动/重命名:`move_item()`使用`rename()`系统调用实现。 6. 树状显示:`show_tree()`递归显示目录结构,使用缩进表示层级关系。 ## 额外注意事项 1. 文件路径处理需要考虑不同操作系统的路径分隔符差异,Windows使用反斜杠`\`,而Unix-like系统使用正斜杠`/`。 2. 错误处理需要更完善,特别是权限不足或文件被占用等情况。 3. 递归删除非空目录功能需要额外实现,当前版本只能删除空目录。 ######[AI写代码神器 | 1797点数解答 | 2025-07-11 01:46:58]
- 批处理文件:依据参数表.xlsx精准修改文件.txt中p、t、s值(GPT | 811点数解答 | 2024-12-05 12:58:37)379
- 用批处理和Python实现“参数表.xlsx”与“文件.txt”数据匹配及修改(讯飞星火 | 531点数解答 | 2024-12-05 13:20:42)367
- 批处理文件:依据参数表.xlsx精准修改文件.txt特定参数值(阿里通义 | 495点数解答 | 2024-12-05 13:22:26)350
- Python实现Q老师成绩管理任务:修改与查看学生成绩(字节豆包 | 191点数解答 | 2025-12-20 20:55:41)70
- Python 代码助力 Q 老师高效管理班级学生成绩:修改与查看一键搞定(字节豆包 | 326点数解答 | 2025-12-27 20:48:12)72
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)345
- 工程师必备!10 种工程日常易用管理工具大揭秘(百度文心 | 346点数解答 | 2023-11-09 18:26:09)290
- Java 实现仿 Windows 资源管理器:文件管理器功能全解析与代码示例(字节豆包 | 623点数解答 | 2025-01-01 14:59:04)247
- Python 利用 win32com 库:将 Excel 转换为 A4 横向 PDF 并单独打印首列(字节豆包 | 507点数解答 | 2025-03-10 15:48:12)304
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)313
- Golang开发必备:两种实现GET操作的神级方法揭秘!(百度文心 | 465点数解答 | 2023-11-09 18:11:57)245
- 仅一次交换,小写字母字符串能否变身周期字符串?(字节豆包 | 189点数解答 | 2024-10-13 15:08:54)313