这里我列出了终端中给出的目录中的所有文件和它们的类型。我想在终端给出一个文件名,如果该文件存在于该文件夹中,我想打印它的名字和路径。我怎样才能做到这一点?
void listDir(char *dirName){
DIR *dir;
struct dirent *dirEntry;
struct stat inode;
char name[1000];
dir=opendir(dirName);
if(dir==0){
printf("Error in opening the directory\n");
exit(-1);
while((dirEntry=readdir(dir))!=0){
sprintf(name, "%s/%s", dirName, dirEntry->d_name);
lstat(name, &inode);
if(S_ISDIR(inode.st_mode)){
printf("This is a directory: ");
else if(S_ISREG(inode.st_mode)){
printf("This is a file: ");
else if(S_ISLNK(inode.st_mode)){
printf("This is a link: ");
printf("%s\n", dirEntry->d_name);
int main(int argc, char **argv){
struct stat fileMetadata;
if(stat(argv[1], &fileMetadata) < 0){
printf("Error in getting information about the file");
exit(-1);
if(S_ISDIR(fileMetadata.st_mode)){ //it's a directory
printf("The content of %s (directory) is:\n", argv[1]);
listDir(argv[1]);
else{
printf("%s it's not a directory\n", argv[1]);
exit(-1);