运行当前位置,显示文件全名,检查是否扩展名多次重叠
该程序是一个文件列表和扩展名检查工具,主要功能包括:
- 列出当前目录下所有文件(跳过"."和".."目录),区分显示文件和目录
- 检查系统是否隐藏文件扩展名,并给出Windows系统下显示扩展名的设置提示
- 当文件名不含点号时提醒用户扩展名可能被隐藏
程序通过标准C库函数实现目录遍历和文件信息获取,最后提示用户按任意键退出。
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>void list_files() {DIR *dir;struct dirent *entry;struct stat file_stat;// 打开当前目录dir = opendir(".");if (dir == NULL) {perror("无法打开目录");return;}printf("当前目录下的文件列表:\n");printf("=====================\n");// 读取目录中的每个条目while ((entry = readdir(dir)) != NULL) {// 跳过 "." 和 ".." 目录if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {continue;}// 获取文件信息if (stat(entry->d_name, &file_stat) == 0) {if (S_ISDIR(file_stat.st_mode)) {printf("[目录] %s/\n", entry->d_name);} else {printf("[文件] %s\n", entry->d_name);}} else {printf("[未知] %s\n", entry->d_name);}}closedir(dir);
}void check_extension_hiding() {printf("\n扩展名隐藏检查:\n");printf("=====================\n");// 在Windows系统中,默认会隐藏已知文件类型的扩展名// 这个函数主要是提醒用户注意这一点printf("注意:在某些操作系统(如Windows)中,已知文件类型的扩展名可能被默认隐藏。\n");printf("请确保在文件资源管理器中启用了显示文件扩展名的选项。\n");printf("在Windows中设置方法:查看 → 显示 → 文件扩展名\n");
}int main() {printf("文件列表和扩展名检查工具\n");printf("==========================\n\n");list_files();check_extension_hiding();printf("\n提示:如果看到文件名中没有点号(.),可能表示扩展名被隐藏。\n");printf("例如:'document' 而不是 'document.txt'\n");puts("任意键退出");getchar();return 0;
}