簡體   English   中英

使用Stat和OpenDir()/ ReadDir()以C語言列出當前目錄和文件類型(Dir,Reg,Lnk)

[英]List Current Directory and File Types (Dir, Reg, Lnk) in C Language using Stat and OpenDir()/ReadDir()

您好,感謝您的閱讀。

我正在嘗試在Linux Shell中運行的用C構建程序,該程序接受一個參數(目錄)並列出該目錄中的所有文件,以及它們是常規文件,鏈接文件還是目錄。 我需要使用OpenDir()/ ReadDir()和stat。 到目前為止,這是我的代碼:(我有點卡住了)目前僅讀取文件並輸出類型。 如何允許我的程序讀取目錄中的所有文件,並輸出其名稱和文件類型?

 #define _BSD_SOURCE
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <time.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <dirent.h>
 #include <time.h>

   int
   main(int argc, char *argv[])
   {
       struct stat sb;

    /*
       if (argc != 2) {
           fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
           exit(EXIT_FAILURE);
       }*/

       if (stat(argv[1], &sb) == -1) {
           perror("stat");
           exit(EXIT_FAILURE);
       }



       switch (sb.st_mode & S_IFMT) {
       case S_IFBLK:  printf("block device\n");            break;
       case S_IFCHR:  printf("character device\n");        break;
       case S_IFDIR:  printf("dir ");                      break;
       case S_IFIFO:  printf("FIFO/pipe\n");               break;
       case S_IFLNK:  printf("lnk ");                      break;
       case S_IFREG:  printf("reg ");                      break;
       case S_IFSOCK: printf("socket\n");                  break;
       default:       printf("unknown?\n");                break;
       }

       printf("%s\n", argv[1]);

       exit(EXIT_SUCCESS);
   }

所以我重寫了所有內容,現在我收到一些有關它是鏈接reg還是dir文件的輸出,但是,我什至只收到文件是目錄,即使它顯然不是目錄(文本文件或.c文件等)也是如此。 )。 這是新代碼:

#define _BSD_SOURCE
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
  #include <sys/types.h>
       #include <time.h>
       #include <stdio.h>
       #include <stdlib.h>


void typeOfFile(struct stat info){

    if (S_ISREG(info.st_mode)) {
        printf("reg");
    }
    if (S_ISDIR(info.st_mode)) {
            printf("dir");
    }
    if (S_ISLNK(info.st_mode)) {
                printf("lnk");
    }

}

int main(int argc, char *argv[]) {

    DIR *dirp;
    struct dirent* dent;
    struct stat info;







typeOfFile(info);

//If no args
    if(argc == 1){

        argv[1] = ".";  
            dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
            do {
                dent = readdir(dirp);
                if (dent)
                {
//////////////////////////////////////////////////////////////////////////
                    if (stat(argv[1], &info) == -1) {
               perror("stat");
               exit(EXIT_FAILURE);
           }



           switch (info.st_mode & S_IFMT) {
           case S_IFBLK:  printf("block device\n");            break;
           case S_IFCHR:  printf("character device\n");        break;
           case S_IFDIR:  printf("dir ");                      break;
           case S_IFIFO:  printf("FIFO/pipe\n");               break;
           case S_IFLNK:  printf("lnk ");                      break;
           case S_IFREG:  printf("reg ");                      break;
           case S_IFSOCK: printf("socket\n");                  break;
           default:       printf("unknown?\n");                break;
           }


//////////////////////////////////////////////////////////////////////////
                    printf("%s \n", dent->d_name);

                }
            } while (dent);
            closedir(dirp);

       }



////////////////////////////////////////////////////////////////////////////////////////////////
//If specified directory    
    if(argc > 1){
        dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
        do {
            dent = readdir(dirp);
            if (dent)
            {

/////////////////////////////////////////////////////////////////////////////////////           
                if (stat(argv[1], &info) == -1) {
               perror("stat");
               exit(EXIT_FAILURE);
           }



           switch (info.st_mode & S_IFMT) {
           case S_IFBLK:  printf("block device\n");            break;
           case S_IFCHR:  printf("character device\n");        break;
           case S_IFDIR:  printf("dir ");                      break;
           case S_IFIFO:  printf("FIFO/pipe\n");               break;
           case S_IFLNK:  printf("lnk ");                      break;
           case S_IFREG:  printf("reg ");                      break;
           case S_IFSOCK: printf("socket\n");                  break;
           default:       printf("unknown?\n");                break;
           }
//////////////////////////////////////////////////////////////////////////////////////
//           printf("%s\n", argv[1]);


                printf("%s \n", dent->d_name);

            }
        } while (dent);
        closedir(dirp);

   } 
return 0;    
}

您的代碼正在使用argv[1] (始終為“。”)調用stat。 -因此,所有項目均被錯誤地識別為目錄。

dent->d_name (文件名)是此處的正確參數。

  if (stat(dent->d_name, &info) == -1) {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM