在linux下用C语言或c++语言遍历指定文件夹下的所有文件
时间: 2023-06-26 08:04:42 浏览: 633
可以使用Linux下的标准库函数dirent.h中的readdir()函数来实现遍历指定文件夹下的所有文件。以下是一个简单的示例代码:
```c
#include <stdio.h>
#include <string.h>
#include <dirent.h>
int main() {
char path[100];
printf("请输入要遍历的文件夹路径:");
scanf("%s", path);
DIR *dir;
struct dirent *ptr;
dir = opendir(path);
if (dir == NULL) {
printf("打开文件夹失败!\n");
return 1;
}
while ((ptr = readdir(dir)) != NULL) {
// 过滤掉父目录和当前目录
if (strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
continue;
printf("%s\n", ptr->d_name);
}
closedir(dir);
return 0;
}
```
该程序会提示用户输入要遍历的文件夹路径,然后利用opendir()打开该文件夹,并通过循环遍历readdir()函数返回的每一个文件名。如果文件名为“.”或“..”,则跳过不处理。
相关问题
C语言实现递归遍历当前文件夹下的所有文件
可以使用C语言中的`opendir`和`readdir`函数实现遍历当前文件夹下的所有文件。具体实现代码如下:
```c
#include <stdio.h>
#include <string.h>
#include <dirent.h>
void traverse_dir(char *dir_path) {
DIR *dir;
struct dirent *entry;
dir = opendir(dir_path);
if (dir == NULL) {
fprintf(stderr, "Failed to open directory\n");
return;
}
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
// Ignore "." and ".." directories
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
// Recursively traverse subdirectory
char sub_dir_path[256];
sprintf(sub_dir_path, "%s/%s", dir_path, entry->d_name);
traverse_dir(sub_dir_path);
} else {
printf("%s/%s\n", dir_path, entry->d_name);
}
}
closedir(dir);
}
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: %s DIRECTORY\n", argv[0]);
return 1;
}
traverse_dir(argv[1]);
return 0;
}
```
这个程序接受一个参数,即要遍历的目录路径,会递归地遍历该目录下的所有文件和子目录,并输出它们的路径。
使用C语言编写一段遍历Linux/proc目录下所有文件的代码
下面是一个遍历Linux `/proc` 目录下所有文件的C语言代码:
```c
#include <stdio.h>
#include <dirent.h>
#include <string.h>
int main() {
DIR* dir = opendir("/proc"); // 打开目录
if (dir == NULL) {
perror("opendir failed");
return -1;
}
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) { // 遍历目录
if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
// 过滤掉 "." 和 ".." 目录
printf("%s\n", entry->d_name);
}
}
closedir(dir); // 关闭目录
return 0;
}
```
此代码使用 `opendir` 打开 `/proc` 目录,然后使用 `readdir` 遍历目录中的所有文件。这里使用 `d_type` 属性过滤掉非目录文件,使用 `strcmp` 函数过滤掉 "." 和 ".." 目录,最后输出剩余的目录名字。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)