我遇到了這篇討論如何從stat庫中列印檔案權限的帖子,但我對結構的定義如何作業感到困惑。
當我定義結構時,我總是將它們定義為struct struct_type *name. 但是在示例中,結構被定義為struct struct_type name(不是指標)。據我所知,應該有兩種不同的方式來訪問資訊,具體取決于您定義結構的方式。
選項一符號
struct stat fileStat; //definition of struct
stat("<filename>", &fileStat); //execute the stat function
fileStat.member_name; //access a member of a struct
選項二表示法
struct stat *fileStat; //definition of struct
stat("<filename>", fileStat); //execute the stat function
fileStat->member_name; //access a member of a struct
但是,當我從提供的示例中的選項一符號切換到選項二時,我得到了非常不同的結果。這兩個選項有什么不同?它不是有效地做同樣的事情嗎?
示例代碼
選項一符號程式
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
struct stat fileStat;
stat("main", &fileStat);
printf("Information for %s\n", argv[1]);
printf("---------------------------\n");
printf("File Size: \t\t%lld bytes\n", fileStat.st_size);
printf("Number of Links: \t%d\n", fileStat.st_nlink);
printf("File inode: \t\t%llu\n", fileStat.st_ino);
printf("File Permissions: \t");
printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");
printf("The file %s a symbolic link\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");
return 0;
}
選項一符號輸出
Information for (null)
---------------------------
File Size: 49960 bytes
Number of Links: 1
File inode: 58527531
File Permissions: -rwxr-xr-x
The file is not a symbolic link
選項二符號程式
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
struct stat *fileStat;
stat("main", fileStat);
printf("Information for %s\n", argv[1]);
printf("---------------------------\n");
printf("File Size: \t\t%lld bytes\n", fileStat->st_size);
printf("Number of Links: \t%d\n", fileStat->st_nlink);
printf("File inode: \t\t%llu\n", fileStat->st_ino);
printf("File Permissions: \t");
printf( (S_ISDIR(fileStat->st_mode)) ? "d" : "-");
printf( (fileStat->st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat->st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat->st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat->st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat->st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat->st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat->st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat->st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat->st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");
printf("The file %s a symbolic link\n", (S_ISLNK(fileStat->st_mode)) ? "is" : "is not");
return 0;
}
選項二符號輸出
Information for (null)
---------------------------
File Size: 5193343115435036343 bytes
Number of Links: 12487
File inode: 930377443599221576
File Permissions: -r-x--x---
The file is not a symbolic link
uj5u.com熱心網友回復:
這不是替代符號
struct stat *fileStat; //definition of struct
stat("<filename>", fileStat); //execute the stat function
fileStat->member_name; //access a member of a struct
這是無效的代碼。您創建了一個不指向任何地方的指標。它需要指向一個統計資料的實體。像這樣
struct stat other_stat;
struct stat *fileStat; //definition of struct
fileStat = &other_stat; // make it point at the instance we just made
stat("<filename>", fileStat); //execute the stat function
fileStat->member_name; //access a member of a struct
或在堆上創建一個
struct stat *fileStat = (struct stat*)malloc(sizeof(struct stat));
stat("<filename>", fileStat); //execute the stat function
fileStat->member_name; //access a member of a struct
在您的“第二個符號”示例中,一旦您這樣做了
stat("filename", fileStat)
所有賭注都取消了。Stat 已將資料寫入未指定的位置。這會導致“未定義的行為”。您可能會很幸運并寫入無法寫入的位置。在這種情況下,您的程式將立即中止。為什么這么幸運?好吧,您會知道出了點問題。最糟糕的行為是您最終寫入一個可寫的位置,并且目前沒有用于其他任何事情。您的程式將運行并且您將交付它,然后客戶使用具有較長檔案名的檔案或在作業系統升級后或在具有不同記憶體量的機器上嘗試它并且“繁榮”它給出奇怪的答案或死亡。這就是為什么存在 Java、c#、go 等語言的原因。以免陷入這種混亂。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425195.html
下一篇:非賦值加法后C指標改變
