调用格式:
#include
#include
#include
int open( const char * pathname, intflags);
int open( const char * pathname, int flags,mode_t mode);
如果文件不存在,则先创建该文件,然后打开。如果操作成功则返回该文件的文件描述符,操作失败则返回-1。其中,参数pathname 指向欲打开的文件路径字符串;flags用于指定文件的打开方式;mode表示文件的权限属性。
#include
#include
#include
#include
#include
#include
int main()
{
int temp;
temp=open("./test",O_RDWR|O_CREAT|S_IRWXU);//不存在则在当前的工作目录创建文件test
printf("%d\n",temp);
temp=close(temp);
printf("%d\n",temp);
exit(0);
}
关闭文件函数
#include
int close(int fd);
关闭文件时会产生一定的影响:
打开一个文件时,该文件描述中的引用计数器值加1,而关闭一个文件会使得该文件描述中的引用计数器值减1,当引用计数器值减为0的时候,系统调用close函数不仅将释放该文件的描述符,而且也将释放该文件所占用的描述表项。此外,关闭一个文件时也释放该进程加在该文件上的所有记录锁。
创建文件函数
调用格式
#include
#include
#include
int creat(const char * pathname,mode_tmode);
利用命令行参数的第二个参数作为pathname参数来创建文件
//创建文件,文件名由argv的第二个参数给出
#include
#include
int main()
{
int temp;
if(argc!=2)
{
printf("arg num error!");
return 1;
}
temp=creat(*(argv+1),S_IRWXU);
printf("%d\n",temp);
return 0;
}
文件偏移定位函数
文件偏移量就是文件指针,是一个非负整数,以度量从文件开始处计算的字节数。
#include
#include
off_t lseek(int fildes,off_t offset ,int whence);
例子
#include
#include
int main(int argc,char *argv[])
{
int temp,seektemp,i,j;
int fileID;
char buf[17]="this is a test!\r\n";
if(argc!=2)
{
printf("arg num error!");
return 1;
}
fileID=open(*(argv+1),O_RDWR|O_CREAT|S_IRWXU);
temp=write(fileID,buf,sizeof(buf));//写入数据
seektemp=lseek(fileID,0,SEEK_CUR);//获得当前的偏移量
for(i=0;i<10;i++)
{
j=sizeof(buf)*(i+1);//计算下一次的偏移量
seektemp=lseek(fileID,j,SEEK_SET);
temp=write(fileID,buf,sizeof(buf));//写入数据
}
close(fileID);
return 0;
}
读文件函数
#include
ssize_t read(int fd,void * buf ,size_tcount);
read()会把参数fd 所指的文件传送count个字节到buf指针所指的内存中。若参数count为0, 则read()不会有作用并返回0。返回值为实际读取到的字节数, 如果返回0, 表示已到达文件尾或是无可读取的数据, 此外文件读写位置会随读取到的字节移动。
#include
#include
#include
#define PERMS 0666
#define DUMMY 0
int main(int argc,char *argv[])
{
int sourcefileID,targetfileID;
int readnum=0;
char buf[1024];
if(argc!=3)
{
printf("arg num error!");
return 1;
}
if((sourcefileID=open(*(argv+1),O_RDONLY,DUMMY))==-1)
{
printf("source file open error!\n");
return 2;
}
if((targetfileID=open(*(argv+2),O_WRONLY|O_CREAT,PERMS))==-1)
{
printf("target file open error!\n");
return 3;
}
while((readnum=read(sourcefileID,buf,1024))>0)
if((write(targetfileID,buf,readnum))!=readnum)//写入的数据和读出的数据不同
{
printf("target file write error!\n");
return 4;
}
close(sourcefileID);
close(targetfileID);
return 0;
}
高级文件操作函数
文件状态操作函数
#include
#include
#include
int stat(const char * file_name,struct stat *buf);
int fstat(int fildes,struct stat *buf);
int lstat (const char * file_name.struct stat * buf);
上述三个函数用于获取文件的属性结构体,成功返回0,失败返回-1.如果目标文件是一个符号链接的时候,lstat返回的是符号链接有关的信息,而stat返回的是符号链接所引用的文件信息。注:Linux下符号链接和符号链接对应的文件可以理解成windows下的快捷方式和快捷方式对应的文件。
#include
#include
#include
#include
int main(int argc,char *argv[])
{
struct stat buf;
stat ("/etc/passwd",&buf);
printf("/etc/passwd file size = %d \n",(int)buf.st_size);
return 0;
}
时间相关函数
#include
#include
int utime(const char * filename,structutimbuf * buf);
utime()用来修改参数filename文件所属的inode存取时间。
在Linux系统中,每个文件都有三个对应的时间信息,分别对应stat结构体中的3个字段。
unsigned longst_atime;
unsigned longst_mtime;
unsigned longst_ctime;
分别表示文件最后访问时间、文件的最后修改时间、文件索引结点(inode)的最后修改时间,对应的操作函数分别为read、write、chmod。
结构utimbuf定义如下
struct utimbuf{
time_t actime;
time_t modtime;
};
如果参数buf为空指针(NULL), 则该文件的存取时间和更改时间全部会设为目前时间。
执行成功则返回0, 失败返回-1, 错误代码存于errno
#include
#include
#include
int main(int argc,char *argv[])
{
int i,fd;
struct stat statbuf;
struct utimbuf timebuf;
for(i=1;i
{
if(stat(argv[i],&statbuf)<0)
{
printf("arg is error\n");
continue;
}
if((fd=open(argv[i],O_RDWR|O_TRUNC))<0)//截断文件
{
printf("open file error\n");
continue;
}
close(fd);
timebuf.actime=statbuf.st_atime;
timebuf.modtime=statbuf.st_mtime;
if(utime(argv[i],&timebuf)<0)
{
printf("reset time error\n");
continue;
}
}
return 0;
}
其他
利用access函数可以测试文件的访问权限,umask函数文件创建屏蔽字,chmod和fchmod函数可以修改文件的权限。
#include
#include
int chmod(const char * path,mode_t mode);
此外,rename函数可以对文件重命名,int rename(const char * oldpath,const char * newpath);
dup和dup2函数可以复制文件的描述符,fcntl函数可以进一步管理低级文件描述符,修改打开文件的性质、复制文件描述符、操作文件锁等。还有目录文件操作函数,mkdir、rmdir、chdir、fchdir和getcwd函数、opendir、closedir、readdir函数等