c语言读取文件夹中的文件名(无文件拓展名)
在做表格时,把一个个文件名复制到对应位置感觉非常繁琐,于是敲了这个代码来节约时间。
#include <stdio.h>
#include <dirent.h>
int main() {
DIR *dir;
struct dirent *ent;
//将所需要查询的文件夹路径放在下方即可
dir = opendir("文件路径");
//注意Windows下路径用'/'隔开
if (dir == NULL) {
printf("无法打开文件夹\n");
return 1;
}
while ((ent = readdir(dir)) != NULL) {
int i = 0;
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
continue;
}
for(i = 0; ent->d_name[i] != '.' ; i++){
printf("%c",ent->d_name[i]);
}
printf("\n");
}
closedir(dir);
return 0;
}