struct stat{
mode_t st_mode; //file type & mode
ino_t st_ino; //i-node number
dev_t st_dev; //device number
dev_t st_rdev; //device number for special files
nlink_t st_nlink; //number of links
uid_t st_uid; //user ID of owner
gid_t st_gid; //group ID of owner
off_t st_size; //size in bytes,for regular files
time_t st_atime; //time of last access
time_t st_mtime; //time of last modification
time_t st_ctime; //time of last file status change
long st_blksize;//best I/O block size
long st_blocks; //number of 512 bytes blocks allocated
};
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
int main()
{
cout << " enter a file name: ";
char name[255];
cin >> name;
struct stat buf;
if(lstat(name,&buf)<0)
{
cout << " file not existing. " << endl;
return -1;
}
if(buf.st_mode & S_IRUSR) cout << " user can read|";
if(buf.st_mode & S_IWUSR) cout << " user can write|";
if(buf.st_mode & S_IXUSR) cout << " user can execute|";
if(buf.st_mode & S_IRGRP) cout << " group can read|";
if(buf.st_mode & S_IWGRP) cout << " group can write|";
if(buf.st_mode & S_IXGRP) cout << " group can execute|";
if(buf.st_mode & S_IROTH) cout << " other can read|";
if(buf.st_mode & S_IWOTH) cout << " other can write|";
if(buf.st_mode & S_IXOTH) cout << " ohter can execute|";
cout << endl;
return 0;
}
如果是想判断某用户对某一文件的访问权限,则可调用access()函数,它的定义是:
access(const char* path, int amode)
其中,amode对应的值有R_OK(可读)、W_OK(可写)、X_OK(可执行)、F_OK(文件存在)。
小结:本文通过对WIN32_FIND_DATA结构和SetFileTime()、SetFileAttributes()等主要函数实现了对磁盘文件的相关属性信息的获取与修改。用此技术可以在通讯等对文件有严格要求的应用领域实现文件全部信息(包括文件内容、文件名以及文件属性等)的完整传送。本文所述程序在Windows 98下由Microsoft Visual C++ 6.0编译调试通过。
int main(void)
{
struct stat statbuf;
FILE *stream;
/* open a file for update */
if ((stream = fopen(FILENAME, "w+")) == NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return(1);
}
/* get information about the file */
stat(FILENAME, &statbuf);
fclose(stream);
/* display the information returned */
if (statbuf.st_mode & S_IFCHR)
printf("Handle refers to a device.\n");
if (statbuf.st_mode & S_IFREG)
printf("Handle refers to an ordinary file.\n");
if (statbuf.st_mode & S_IREAD)
printf("User has read permission on file.\n");
if (statbuf.st_mode & S_IWRITE)
printf("User has write permission on file.\n");
printf("Drive letter of file: %c\n", 'A'+statbuf.st_dev);
printf("Size of file in bytes: %ld\n", statbuf.st_size);
printf("Time file last opened: %s\n", ctime(&statbuf.st_ctime));
return 0;
}