andylin02 发表于 2013-1-29 23:07:53

获取文件访问类型

#include <sys/types.h>#include <fcntl.h>#include <iostream>using namespace std;int main(int argc, char* argv[]){intnMode = 0;intnVal = 0;if (2 != argc){cout <<"usage: file_info.exe <descriptor>";return -1;}if ((nVal = fcntl(atoi(argv), F_GETFL, 0)) < 0){cout << "fcntl error for fd %d" << atoi(argv) << endl;return -1;}nMode = nVal & O_ACCMODE;switch (nMode){case O_RDONLY:cout << "read only" << endl;break;case O_WRONLY:cout << "write only" << endl;break;case O_RDWR:cout << "read & write" << endl;break;default:cout << "unknown access mode" << endl;break;}if (nVal & O_APPEND){cout << "append" << endl;}if (nVal & O_NONBLOCK){cout << "nonblocking" << endl;}if (nVal & O_SYNC){cout << "synchronous writes" << endl;}return 0;}
页: [1]
查看完整版本: 获取文件访问类型