【发布时间】:2019-05-10 19:18:35
【问题描述】:
我想在一个目录及其文件和子目录上递归运行。假设该目录可以包含任何文件之王(c,txt,python ....)检查当前文件是否为c文件,如果是则编译它。 这是我目前所拥有的:
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdlib.h>
#include<errno.h>
void listdir(const char *name, int indent)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
printf("%*s[%s]\n", indent, "", entry->d_name);
listdir(path, indent + 2);
} else {
printf("%*s- %s\n", indent, "", entry->d_name);
}
}
closedir(dir);
}
int main(void) {
listdir(".", 0);
return 0;
}
如何检查文件是否为 c 文件?以及如何使用代码编译它? 任何帮助将不胜感激。
【问题讨论】:
-
检查文件扩展名应该没问题。然后在文件上运行
system("gcc -c ") -
OT:为了便于阅读和理解:始终缩进代码。在每个左大括号“{”后缩进。在每个右大括号 '}' 之前取消缩进。建议每个缩进级别为 4 个空格
-
在调用
gcc时,始终启用警告。对于gcc,至少使用:-c -Wall -Wextra -Wconversion -pedantic -std=gnu11
标签: c file compilation operating-system system-calls