文件属性操作
1.获取文件属性
stat/fstat/lstat函数
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *file_name,struct stat *buf);
int fstat(int filedes,struct stat *buf);
int lstat(const char *file_name,struct stat *buf);
struct stat {
dev_t st_dev; //文件的设备编号
ino_t st_ino; //节点
mode_t st_mode; //文件的类型和存取的权限
nlink_t st_nlink; //连到该文件的硬连接数目,刚建立的文件值为1
uid_t st_uid; //用户ID
gid_t st_gid; //组ID
dev_t st_rdev; //(设备类型)若此文件为设备文件,则为其设备编号
off_t st_size; //文件字节数(文件大小)
unsigned long st_blksize; //块大小(文件系统的I/O 缓冲区大小)
unsigned long st_blocks; //块数
time_t st_atime; //最后一次访问时间
time_t st_mtime; //最后一次修改时间
time_t st_ctime; //最后一次改变时间(指属性)
};
st_mode包含的文件类型信息,POSIX标准定义了一系列的宏
S_ISLNK:判断是否为符号链接。
S_ISREG:判断是否为一般文件。
S_ISDIR:判断是否为目录文件。
S_ISCHR:判断是否为字符设备文件。
S_ISBLK:判断是否为块设备文件。
S_ISFIFO:判断是否为先进先出FIFO。
S_ISSOCK:判断是否为socket。
设置文件属性
1.hmod/fchmod
2.chown/fchown/lchown
#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);
chown会将参数path指定的文件所有者id变更为参数owner代表的用户id,而将该文件所有者的组id变更为参数group组id。
目录的创建与删除
#include <sys/stat.h>
#include <sys/types.h>\
int mkdir(const char *pathname,mode_t mode);//创建
int rmdir(const char *pathname);//删除
获得当前目录
每个进程都有一个当前目录,此目录是搜索所有相对路径名的起点。
getcwd函数
#include <unistd.h>
char *getcwd(char *buf,size_t size);
getcwd会将当前的工作目录绝对路径复制到参数buf所指的内存空间,参数size为buf的空间大小。若工作目录绝对路径的字符串长度超过参数size大小,返回值为NULL,errno值为ERANGE。
设置工作目录
chdir函数
#include <unistd.h>
int chdir(const char *path);
chdir用来将当前工作目录改为由参数path指定的目录。执行成功返回0,有错误发生返回-1。
获取目录信息
1.opendir函数
#include <sys/types.h>
#include <dirent.h>
DIR ×opendir(const char *name);
opendir用来打开参数name指定的目录,并返回DIR*形态的目录流。
2.readdir函数
#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dir);
readdir用来从参数dir所指向的目录中读取出目录项信息,返回一个struct dirent结构的指针。
struct dirent
{
long d_ino; /* inode number 索引节点号 */
off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
unsigned short d_reclen; /* length of this d_name 文件名长 */
unsigned char d_type; /* the type of d_name 文件类型 */
char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长256字符 */
}
3.closedir
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dir);
closedir用来关闭参数dir指向的目录,执行成功返回0,有错误回-1。
show_files.c
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<dirent.h>
#include<stdlib.h>
int my_readir(const char *path)
{
DIR *dir;
struct dirent *ptr;
if((dir = opendir(path)) == NULL)
{
perror("opendir");
return -1;
}
while((ptr = readdir(dir)) != NULL)
{
printf("file name : %s\n",ptr->d_name);
}
closedir(dir);
return 0;
}
int main(int argc , char **argv)
{
if(argc<2)
{
printf("listfile <target path>\n");
exit(1);
}
if(my_readir(argv[1])<0)
exit(1);
return 0;
}
本程序可以将命令行的参数制定的目录下的文件显示出来。
1.open(函数)
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *pathname, int flags);
int open(const char *pathname,int flags,mode_t mode);
pathname是要打开或创建的含路径的文件名,第二个参数flags表示打开文件的方式。
O_RDONLY:以只读方式打开文件。
O_WRONLY:以只写方式打开文件。
O_RDWR:以可读可写的方式打开文件。
这三种打开方式是互斥的。
O_CREAT:若文件不存在则自动建立该文件,只有在此时,才需要用到第三个参数mode,以说明新文件的存取权限。
O_EXCL:如果O_CREAT也没设置,此指令会去检查文件是否存在。文件不存在则创建文件,文件存在则导致打开文件错误。
O_TRUNC:若文件存在并且以可写的方式打开时,此标志将文件长度清为零,即数据丢失,文件属性不变。
O_APPEND:所写入的数据会以追加的方式加入到文件后面。
2.close函数
#include <unistd.h>
int close(int fd);
close函数只有一个参数,此参数标示需要关闭的文件的文件描述符,该文件描述符是由open函数或creat函数得到。当close调用成功时,返回值为0,发生错误返回-1,并设哦宁国置错误代码。(注意,close函数调用成功并不保证数据能全部写回硬盘)
2.文件的读写
read函数
#include <unistd.h>
ssize_t read(int fd ,void *buf,size_t count)
从文件描述符fd所指向的文件中读取count个字节的数据到buf所指向的缓存中。若参数count为0,则read不会读取数据,只返回0。
write函数
#include <unistd.h>
ssize_t write(int fildes,off_t offset,int whence);
将buf所指向的缓冲区中的count个字节数据写入到由文件描述符fd所指示的文件中。调用成功,返回写入字节数,发生错误,返回-1。