C语言将案例扫描当前目录,得到所有文件名(只输出.c)
时间: 2024-05-07 15:17:46 浏览: 14
可以使用C语言中的dirent.h头文件中的函数来实现该功能。具体代码如下:
```c
#include <stdio.h>
#include <dirent.h>
#include <string.h>
int main(void) {
DIR *dir;
struct dirent *ent;
if ((dir = opendir(".")) != NULL) {
while ((ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name + strlen(ent->d_name) - 2, ".c") == 0) {
printf("%s\n", ent->d_name);
}
}
closedir(dir);
} else {
perror("");
return 1;
}
return 0;
}
```
该程序会打开当前目录,遍历其中的所有文件,如果文件名以".c"结尾,则将其输出。
相关问题
用c语言使用递归方法实现,向终端输出指定目录下所有子目录的文件名
好的,下面是使用递归方法实现输出指定目录下所有子目录的文件名的 C 语言代码示例:
```c
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
void print_files(char *dir_path)
{
DIR *dir;
struct dirent *entry;
struct stat statbuf;
char file_path[256];
if ((dir = opendir(dir_path)) == NULL) {
fprintf(stderr, "Error: cannot open directory %s\n", dir_path);
return;
}
while ((entry = readdir(dir)) != NULL) {
sprintf(file_path, "%s/%s", dir_path, entry->d_name);
if (lstat(file_path, &statbuf) < 0) {
fprintf(stderr, "Error: cannot stat file %s\n", file_path);
continue;
}
if (S_ISDIR(statbuf.st_mode)) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
printf("directory: %s\n", file_path);
print_files(file_path);
} else {
printf("file: %s\n", file_path);
}
}
closedir(dir);
}
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "Usage: %s <directory>\n", argv[0]);
return 1;
}
print_files(argv[1]);
return 0;
}
```
在这个示例中,我们使用 `opendir()` 函数打开指定目录,然后使用 `readdir()` 函数遍历目录中的所有文件和子目录。对于每个文件或子目录,我们使用 `lstat()` 函数获取其详细属性信息,判断其是否为子目录。如果是子目录,则递归调用 `print_files()` 函数,继续遍历该子目录下的所有文件和子目录;如果是普通文件,则直接输出文件路径。
需要注意的是,在递归调用 `print_files()` 函数时,需要传入子目录的路径,而不是子目录的文件名。另外,为了防止无限递归,我们需要排除子目录中的 `.` 和 `..` 文件。
最后,在 `main()` 函数中,我们检查是否传入了指定目录的路径参数,如果没有则输出使用方法。然后调用 `print_files()` 函数,传入指定目录的路径即可。
c++ 删除一个目录下指定文件名的1.jpg和1.dat文件,不用c++17
如果您的编译器不支持C++17,您可以使用旧的文件操作函数来删除一个目录下指定文件名的1.jpg和1.dat文件。以下是一个示例代码:
```cpp
#include <iostream>
#include <string>
#include <dirent.h>
#include <unistd.h>
void deleteFiles(const std::string& directory, const std::string& filename) {
DIR* dir = opendir(directory.c_str());
if (dir == nullptr) {
std::cerr << "Failed to open directory: " << directory << std::endl;
return;
}
dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
std::string file = entry->d_name;
if (file.find(filename) != std::string::npos && (file == "1.jpg" || file == "1.dat")) {
std::string filepath = directory + "/" + file;
if (unlink(filepath.c_str()) == 0) {
std::cout << "Deleted: " << filepath << std::endl;
} else {
std::cerr << "Failed to delete file: " << filepath << std::endl;
}
}
}
closedir(dir);
}
int main() {
std::string directory = "path/to/your/directory";
std::string filename = "1";
deleteFiles(directory, filename);
return 0;
}
```
您只需将代码中的 `"path/to/your/directory"` 替换为您想要删除文件的目录路径,将 `"1"` 替换为您指定的文件名。此代码会打开目录,遍历目录下的所有文件,检查文件名是否包含指定的文件名,并且文件名为"1.jpg"或"1.dat",如果是则删除该文件。
请注意,此代码使用了C语言的文件操作函数(opendir、readdir、unlink),并结合C++进行了封装。这种方法在早期的C++标准中是常用的文件操作方式。然而,这些函数在C++17中被新的文件系统库所取代,更推荐使用新的标准库来进行文件操作。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)