我有这个代码输出当前目录中的文件和目录名称,如何从指定目录中打印仅文件
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
int main (int c, char *v[]) {
DIR * d;
char * dir_name = ".";
/* Open the current directory. */
d = opendir (dir_name);
if (! d) {
fprintf (stderr, "Cannot open directory '%s': %s\n",
dir_name, strerror (errno));
exit (EXIT_FAILURE);
}
while (1) {
struct dirent * entry;
entry = readdir (d);
if (! entry) {
break;
}
printf ("%s\n", entry->d_name);
}
/* Close the directory. */
if (closedir (d)) {
fprintf (stderr, "Could not close '%s': %s\n",
dir_name, strerror (errno));
exit (EXIT_FAILURE);
}
return 0;
}
我已经改变了
d = opendir (dir_name)
到
d = opendir (v[1])
它打开了我需要的目录,但它不打印文件,它会打印该目录中的所有内容。