当前目录上查找文件名并返回函数
当前目录下查找文件名并返回函数。
linux有没有函数库函数 可以实现在指定的目录下,查找规定类型的文件,并把文件该文件名及后缀名返回.
相当于 find -name "*.ini" 然后就返回了 config.ini
------解决方案--------------------
那么就调用这个命令,输出重定向到文件,然后读文件输出就是了。
------解决方案--------------------
linux有没有函数库函数 可以实现在指定的目录下,查找规定类型的文件,并把文件该文件名及后缀名返回.
相当于 find -name "*.ini" 然后就返回了 config.ini
------解决方案--------------------
那么就调用这个命令,输出重定向到文件,然后读文件输出就是了。
------解决方案--------------------
- C/C++ code
#include <stdio.h> #include <stdlib.h> #include <sys/dir.h> #include <sys/stat.h> #include <string.h> //判断是否为目录 int IS_DIR(const char* path) { struct stat st; lstat(path, &st); return S_ISDIR(st.st_mode); } //遍历文件夹de递归函数 void List_Files_Core(const char *path, int recursive) { DIR *pdir; struct dirent *pdirent; char temp[256]; pdir = opendir(path); if(pdir) { while(pdirent = readdir(pdir)) { //跳过"."和".." if(strcmp(pdirent->d_name, ".") == 0 || strcmp(pdirent->d_name, "..") == 0) continue; sprintf(temp, "%s/%s", path, pdirent->d_name); printf("%s\n", temp); //当temp为目录并且recursive为1的时候递归处理子目录 if(IS_DIR(temp) && recursive) { List_Files_Core(temp, recursive); } } } else { printf("opendir error:%s\n", path); } closedir(pdir); } //遍历文件夹的驱动函数 void List_Files(const char *path, int recursive) { int len; char temp[256]; //去掉末尾的'/' len = strlen(path); strcpy(temp, path); if(temp[len - 1] == '/') temp[len -1] = '\0'; if(IS_DIR(temp)) { //处理目录 List_Files_Core(temp, recursive); } else //输出文件 { printf("%s\n", path); } } int main(int argc, char** argv) { if(argc != 2) { printf("Usage: ./program absolutePath\n"); exit(0); } List_Files(argv[1], 1); return 0; }
------解决方案--------------------
要么调用find,要么写find的代码。