簡體   English   中英

使用stat(C)查看文件的屬性

[英]Viewing attributes of a file with stat (C)

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>


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

    struct stat *buf;
    buf = malloc(sizeof(struct stat)); 

    DIR * current_directory_ptr; /* DIR is a type from dirent.h */
    struct dirent * next_entry_ptr; /* struct dirent is a type from dirent.h */

    char* dirToView [200];

    printf("Enter path of desired directory\n");
    scanf("%s)", &dirToView);

    current_directory_ptr = opendir(dirToView);

    next_entry_ptr = readdir (current_directory_ptr);

    while(next_entry_ptr != NULL){
        printf("File has inode number %d and is called %s \n", 
             (int) next_entry_ptr ->d_ino, next_entry_ptr->d_name);
        next_entry_ptr=readdir(current_directory_ptr);

    }

    char* fileToView [200];
    printf("Enter name of desired file\n");
    scanf("%s)", &fileToView);



    stat(fileToView, buf);
    off_t size = buf -> st_size;
    printf("Size = %ld \n", size);

    uid_t owner = buf ->st_uid;
    printf("owner = %d \n", owner);


    closedir(current_directory_ptr);


    return (EXIT_SUCCESS);
}

該代碼的目的是使用掃描來獲取和輸出所選文件的詳細信息。 打開目錄是可行的,但是在打開文件時,無論該文件的實際值如何,大小和所有者的結果均為0。 我相信這是因為我將它們打印為錯誤的類型,但是我不確定。 輸出stat()結果的正確系統是什么?

編輯清晰

問題在於下面突出顯示的行

  char* fileToView [200]; printf("Enter name of desired file\\n"); scanf("%s)", &fileToView); stat(fileToView, buf); off_t size = buf -> st_size; printf("Size = %ld \\n", size); uid_t owner = buf ->st_uid; printf("owner = %d \\n", owner); 

程序中較早的代碼按預期運行,至少有一個響應已指向較早的行。 對於任何不明確的內容,我深表歉意。

打印未知大小整數的正確方法是使用%jd / %ju ,中間轉換為intmax_t / uintmax_t

printf("File has inode number %jd and is called %s\n", 
    (intmax_t) next_entry_ptr ->d_ino, next_entry_ptr->d_name);

intmax_t / uintmax_t被定義為最大整數有符號/無符號類型,可以表示任何其他整數類型的值。

強制轉換實際上將整數放大到最大大小,然后允許您以可移植的方式匹配格式說明符和類型。

您確實應該編譯所有警告和調試信息(如果使用GCC gcc -Wall -Wextra -g ),並閱讀函數scanf(3)stat(2)的文檔。 請注意,兩者都可能失敗,您應該對此進行測試。

您可以嘗試,而不是

/// BAD CODE
char* fileToView [200];
printf("Enter name of desired file\n");
scanf("%s)", &fileToView);
stat(fileToView, buf);
off_t size = buf -> st_size;
printf("Size = %ld \n", size);

更好的東西是:

char fileToView[200];
memset (fileToView, 0, sizeof(fileToView));
printf("Enter name of desired file:\n");
if (scanf("%199s", fileToView)<=0) {
   perror("scanf"); exit(EXIT_FAILURE); }
struct stat mystat;
memset (&mystat, 0, sizeof(mystat));
if (stat(fileToView, &mystat)) {
  fprintf(stderr, "stat '%s' failed: %s\n", fileToView, strerror(errno));
  exit(EXIT_FAILURE);
}
printf("for file '%s' size = %ld\n", fileToView, (long) mystat.st_size);

注意,我正在測試scanf成功; 我將名稱輸入限制為199( %s將不接受任何空格,因為它會停止掃描它們); 我將mystat分配為局部變量(無需對其進行堆分配),並且正在測試stat失敗; 您的fileToView被錯誤地聲明為200個指向char指針的數組。 我也有將所有局部變量歸零的習慣。

順便說一句,您可以嘗試strace(1)您的程序。 您將了解系統調用它正在做什么。

暫無
暫無

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

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