void traverseDir(string path){ DIR *dir = opendir(path.c_str()); // 打开目录 if(!dir){ cerr << "Failed to open directory " << path << endl; return; } struct dirent *entry; while((entry = readdir(dir)) != NULL){ // 遍历目录 string name = entry->d_name; if(name == "." || name == "..") continue; string fullpath = path + "/" + name; struct stat st; if(stat(fullpath.c_str(), &st) == -1) continue; if(S_ISDIR(st.st_mode)){ // 目录 cout << "[dir] " << name << endl; traverseDir(fullpath); // 递归遍历子目录 } else if(S_ISREG(st.st_mode)){ // 文件 cout << "[file] " << name << endl; } } closedir(dir); }解释代码
时间: 2024-03-04 11:52:04 浏览: 159
这段代码是 C++ 实现的遍历指定目录下所有文件和子目录的函数。它接受一个字符串参数 path,表示要遍历的目录路径。函数首先通过 opendir 函数打开目录,如果打开失败则输出错误信息并返回。接着使用 readdir 函数遍历目录,对于每个目录项,如果是 "." 或 ".." 则跳过,否则将目录项名加上路径后得到目录项的完整路径 fullpath,并通过 stat 函数获取文件状态信息。如果是目录则输出目录信息并递归调用 traverseDir 函数遍历子目录,否则输出文件信息。最后使用 closedir 函数关闭目录。
相关问题
1.创建文件夹: #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <iostream> using namespace std; int main() { string folder_name = "new_folder"; mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //创建文件夹 return 0; } 2.复制文件: #include <stdio.h> #include <stdlib.h> int main() { FILE *fp1, *fp2; //定义两个文件指针 char ch; fp1 = fopen("file1.txt", "r"); //打开要复制的文件 fp2 = fopen("file2.txt", "w"); //打开要复制到的文件 while ((ch = fgetc(fp1)) != EOF) { fputc(ch, fp2); //复制文件 } fclose(fp1); fclose(fp2); return 0; } 3.移动文件: #include <stdio.h> #include <stdlib.h> int main() { char old_path[100] = "old_folder/file1.txt"; char new_path[100] = "new_folder/file1.txt"; int result = rename(old_path, new_path); //移动文件 if (result == 0) { printf("移动成功\n"); } else { printf("移动失败\n"); } return 0; } 4.删除文件夹: #include <unistd.h> #include <stdio.h> int main() { char folder_name[100] = "new_folder"; int result = rmdir(folder_name); //删除文件夹 if (result == 0) { printf("删除成功\n"); } else { printf("删除失败\n"); } return 0; } 5.显示文件夹中的内容: #include <dirent.h> #include <stdio.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { printf("%s\n", ent->d_name); //遍历文件夹中的文件 } closedir(dir); return 0; } 6.查看文件内容: #include <stdio.h> int main() { FILE *fp; char ch; fp = fopen("file1.txt", "r"); //打开文件 while ((ch = fgetc(fp)) != EOF) { printf("%c", ch); //输出文件内容 } fclose(fp); return 0; } 7.修改文件权限: #include <sys/stat.h> #include <stdio.h> int main() { char file_name[100] = "file1.txt"; chmod(file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); //修改文件权限 return 0; } 8.搜索文件: #include <dirent.h> #include <stdio.h> #include <string.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; char search_name[100] = "file1.txt"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { if (strcmp(ent->d_name, search_name) == 0) //搜索文件 { printf("找到文件:%s\n", ent->d_name); break; } } closedir(dir); return 0; }将上述代码整合成一个完整的程序代码
以下是将上述代码整合成一个完整的程序代码:
```
#include <iostream>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
using namespace std;
// 1.创建文件夹
void create_folder()
{
string folder_name = "new_folder";
mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //创建文件夹
}
// 2.复制文件
void copy_file()
{
FILE *fp1, *fp2; //定义两个文件指针
char ch;
fp1 = fopen("file1.txt", "r"); //打开要复制的文件
fp2 = fopen("file2.txt", "w"); //打开要复制到的文件
while ((ch = fgetc(fp1)) != EOF)
{
fputc(ch, fp2); //复制文件
}
fclose(fp1);
fclose(fp2);
}
// 3.移动文件
void move_file()
{
char old_path[100] = "old_folder/file1.txt";
char new_path[100] = "new_folder/file1.txt";
int result = rename(old_path, new_path); //移动文件
if (result == 0)
{
printf("移动成功\n");
}
else
{
printf("移动失败\n");
}
}
// 4.删除文件夹
void delete_folder()
{
char folder_name[100] = "new_folder";
int result = rmdir(folder_name); //删除文件夹
if (result == 0)
{
printf("删除成功\n");
}
else
{
printf("删除失败\n");
}
}
// 5.显示文件夹中的内容
void show_folder_content()
{
DIR *dir;
struct dirent *ent;
char folder_name[100] = "new_folder";
dir = opendir(folder_name); //打开文件夹
while ((ent = readdir(dir)) != NULL)
{
printf("%s\n", ent->d_name); //遍历文件夹中的文件
}
closedir(dir);
}
// 6.查看文件内容
void show_file_content()
{
FILE *fp;
char ch;
fp = fopen("file1.txt", "r"); //打开文件
while ((ch = fgetc(fp)) != EOF)
{
printf("%c", ch); //输出文件内容
}
fclose(fp);
}
// 7.修改文件权限
void change_file_permission()
{
char file_name[100] = "file1.txt";
chmod(file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); //修改文件权限
}
// 8.搜索文件
void search_file()
{
DIR *dir;
struct dirent *ent;
char folder_name[100] = "new_folder";
char search_name[100] = "file1.txt";
dir = opendir(folder_name); //打开文件夹
while ((ent = readdir(dir)) != NULL)
{
if (strcmp(ent->d_name, search_name) == 0) //搜索文件
{
printf("找到文件:%s\n", ent->d_name);
break;
}
}
closedir(dir);
}
int main()
{
create_folder();
copy_file();
move_file();
delete_folder();
show_folder_content();
show_file_content();
change_file_permission();
search_file();
return 0;
}
```
编写C或C++语言程序,实现:“/root”路径和“/home”路径下目录树的遍历。
好的,以下是一个简单的程序,可以遍历/root和/home路径下的目录树:
```c++
#include <iostream>
#include <dirent.h>
#include <string.h>
void traverse(const char* dir_path);
int main() {
traverse("/root");
traverse("/home");
return 0;
}
void traverse(const char* dir_path) {
DIR* dir = opendir(dir_path);
if (dir == nullptr) {
std::cerr << "Failed to open directory: " << dir_path << std::endl;
return;
}
dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
// Ignore "." and ".." directories
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
// Print the name of the entry
std::cout << entry->d_name << std::endl;
// If the entry is a directory, recursively traverse it
if (entry->d_type == DT_DIR) {
std::string subdir_path = std::string(dir_path) + "/" + entry->d_name;
traverse(subdir_path.c_str());
}
}
closedir(dir);
}
```
该程序使用了C++的标准库,以及Linux系统下的dirent.h头文件来实现目录遍历。在main函数中分别调用了traverse函数遍历/root和/home目录,traverse函数会递归遍历所有子目录,并打印出每个目录和文件的名称。
阅读全文