写一段代码:根据文件名搜索文件
时间: 2023-03-28 16:04:32 浏览: 72
根据文件名检索文件Java实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
int main(int argc, char *argv[]) {
DIR *dir;
struct dirent *entry;
char *filename = argv[1];
int found = ;
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[]);
exit(1);
}
dir = opendir(".");
if (dir == NULL) {
perror("opendir");
exit(1);
}
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, filename) == ) {
printf("Found file: %s\n", filename);
found = 1;
break;
}
}
if (!found) {
printf("File not found: %s\n", filename);
}
closedir(dir);
return ;
}
阅读全文