一、获取文件和目录的信息
#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *pathname, struct stat * buf) ;
int fstat(int fd,struct stat * buf) ;
int lstat(const char *pathname, struct stat * buf) ;
二、文件类型
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int i;
struct stat buf;
char* ptr;
for(i=1; i<argc; i++)
{
cout << argv[i] << ":";
if(lstat(argv[i], &buf)<0)
{
cout << "lstat error" << endl;
continue;
}
if( S_ISREG(buf.st_mode))
ptr = "reg";//普通文件
else if(S_ISDIR(buf.st_mode))
ptr = "dir";//目录
else if(S_ISCHR(buf.st_mode))
ptr = "chr";//特殊字符文件
else if(S_ISBLK(buf.st_mode))
ptr = "blk";//块儿设备
else if(S_ISFIFO(buf.st_mode))
ptr = "fifo";//管道
#ifdef S_ISLNK
else if(S_ISLNK(buf.st_mode))
ptr = "lnk";//链接文件
#endif
#ifdef S_ISSOCK
else if(S_ISSOCK(buf.st_mode))
ptr = "sock";//目录
#endif
else
ptr = "unknown type";
cout << ptr << endl;
}
}
三、许可权测试
#include <unistd.h>
//mode:R_OK 可读, W_OK 可写, X_OK 可执行, F_OK 文件是否存在
int access(const char *pathname, int mode ) ;
四、修改存取权限
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *path, mode_t mode) ;
int fchmod(int fd, mode_t mode) ;
五、更改文件的用户ID和组ID
#include <sys/types.h>
#include <unistd.h>
int chown(const char *path, uid_t owner, gid_t group) ;
int fchown(int fd, uid_t owner, gid_t group) ;
int lchown(const char * path, uid_t owner, gid_t group) ;
六、文件截断
#include <sys/types.h>
#include <unistd.h>
int truncate(const char *path, off_t length) ;
int ftruncate(int fd, off_t length) ;
七、符号连接
符号连接时对一个文件的间接指针
八、文件时间
st_atime 文件数据的最后存取时间read -u
st_mtime 文件数据的最后修改时间 write 缺省
st_ctime i节点状态的最后更改时间 chmod, chown - c
#include <sys/types.h>
#include <utime.h>
int utime(const char* path, const struct utimbuf* time )
->剩余9995小时30分钟