2012-08-23 152 views
7

在linux中,當stat()與斷開的鏈接文件一起使用時,它會因爲-1而失敗。所以我用lstat()成功了。什麼是Windows中的lstat()替代方法?

對於窗口中的相同情況,_stat()失敗且斷開快捷方式,但窗口中沒有_lstat()。請幫助找到在Windows中lstat()的替代方案。

回答

7

GetFileAttributesGetFileAttributesEx很可能(如果我理解statlstat正確)。從文檔引用:

符號鏈接行爲 - 如果路徑指向符號鏈接,則該函數返回符號鏈接的屬性。

+0

非常感謝..讓我嘗試和檢查 –

3

hey _stat()或stat()在破損的快捷鍵上也能正常工作。這就是原因,在Windows中沒有其他選擇像lstat(UNIX)。

在Unix中,stat()失敗且鏈接中斷,因此提供lstat來解決問題。

謝謝大家的幫助。

+0

我假設的問題,而這個答案,在MSVC的運行庫的情況下作了?無論如何,謝謝發佈這個答案,因爲它讓我不知道爲什麼人們會爲了做一個'_stat()'而遵循快捷方式,而不是創建一個'_lstat()',而不是這樣做用它。我的意思是,前者的工作量很大,以至於任何有足夠聰明的人都應該在這個過程中有足夠的時間去認識到他們也需要這樣做。 – SamB

6

接受的答案不提供完整的stat等效。所述stat結構被定義爲

struct stat { 
       dev_t  st_dev;  /* ID of device containing file */ 
       ino_t  st_ino;  /* inode number */ 
       mode_t st_mode; /* protection */ 
       nlink_t st_nlink; /* number of hard links */ 
       uid_t  st_uid;  /* user ID of owner */ 
       gid_t  st_gid;  /* group ID of owner */ 
       dev_t  st_rdev; /* device ID (if special file) */ 
       off_t  st_size; /* total size, in bytes */ 
       blksize_t st_blksize; /* blocksize for filesystem I/O */ 
       blkcnt_t st_blocks; /* number of 512B blocks allocated */ 
       time_t st_atime; /* time of last access */ 
       time_t st_mtime; /* time of last modification */ 
       time_t st_ctime; /* time of last status change */ 
      }; 

GetFileAttributes..不提供任何所有者信息(它在一個WIN32_FIND_DATA對象返回數據)。如果你需要這個所有者信息,它看起來像你可以使用GetSecurityInfo [1]。

[1] https://msdn.microsoft.com/en-us/library/windows/desktop/aa446629%28v=vs.85%29.aspx

相關問題