[C、C++] UNIX 或 Linux環境中,取得檔案資訊
在撰寫C C++ 常常會有需要判斷一個檔案,目前的狀態,是不是資料夾、檔案類型、檔案模式的狀況。
在需要這些資訊的時候
您就可以使用
#include <sys/stat.h>
stat 這一個Lib。
透過stst structure 及 fstat(), lstat(), and stat(),
這三個function的配合,
就可以取得檔案的類型、檔案的模式等等資訊。
更多的資訊,您可以參考以下的這個連結
http://www.opengroup.org/pubs/online/7908799/xsh/sysstat.h.html
在這邊提供一個判斷一個檔案是否為資料夾的小程式
來供給大家參考。
#include <sys/stat.h>
bool isFileSelected(string filename) {
struct stat fileState;
if(filename=="") {
//No file selected!
return false;
}
//stat() is define in <sys/stat.h>.
if(!stat(filename.c_str(), &fileState)) {
if(!S_ISDIR(fileState.st_mode)) {
//It's not a directory.
return true;
}
}
return false;
}
相關文章:
[C、C++] 計算檔案大小程式
[IT知識] Fedora Core 7 的KVM功能?
[軟體推薦] 如Vista Aero 即時工作列縮圖的Visual Task Tips
[軟體推薦] 方便切換網路設定的 Net Profiles
[WinXP] 解決休眠時出現系統資源不足問題!
隨機文章
Powered by Stuff-a-Blog



