文件I/O操作

零、文件操作符

文件操作符是一个非负证书,是一个用于描述被打开文件的索引值

标准输入stdin、标准输出stdout和标准出错stderr的索引值是0、1、2


一、文件的操作

1、文件的创建、打开与关闭

(1)文件的打开

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname,int flags)
int open(const char *pathname,int flags,mode_t mode)
open函数返回,若成功返回文件描述符,若出错返回-1
pathname使用绝对路径或相对路径

flags是打开文件方式的参数,定义在fcntl.h文件中,O_RDONLY,O_WRONLY,O_RDWR三个参数只能指定其中一个

flags取值含义
O_RDONLY以只读方式打开文件
O_WRONLY以只写方式打开文件
O_RDWR以读写方式打开文件
O_CREATE若打开文件不存在则创建文件。使用此选项需要同时使用第三个参数mode说明该文件的存取许可
O_EXCL如果同时指定了O_CREATE,而文件已经存在,则导致调用出错
O_TRUNC如果文件存在,并且为只读或只写方式打开,则将其长度截断为0
O_NOCTTY如果pathname指的是终端tty,则不将此设备分配作为此进程的控制终端
O_APPEND每次写时都加到文件的尾端
O_NONBLOCK如果pathname指的是一个FIFO、一个块特殊文件或一个字符特殊文件,则选择项为此文件的本次打开操作和后续的I/O操作设置为非阻塞方式
O_NONELAY同O_NONBLOCK
O_SYNC只在数据被写入外存或其他设备之后操作才返回

mode是用于指定所创建文件的权限,使用|来组合

mode取值对应八进制数含义
S_ISUID04000设置用户识别号
S_ISGID02000设置组织别号
S_SVTX01000粘帖位
S_IRUSR00400文件所有者的读权限位
S_IWUSR00200文件所有者的写权限位
S_IXUSR00100文件所有者的执行权限位
S_IRGRP00040所有者同组用户的读权限位
S_IWGRP00020所有者同组用户的写权限位
S_IXGRP00010所有者同组用户的执行权限位
S_IROTH00004其他用户的读权限位
S_IWOTH00002其他用户的写权限位
S_IXOTH00001其他组用户的执行权限位
S_IRWXU S_IRUSR|S_IWUSR|S_IXUSR,文件所有者的读、写、执行
S_IRWXG S_IRGRP|S_IWGRP|S_IXGRP,文件所有者同组用户的读、写、执行
S_IRWXO S_IROTH|S_IWOTH|S_IXOTH,其他组用户的读、写、执行

(2)creat函数

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat (const char *pathname,mode_t mode)
create函数返回,成功返回只写方式打开的文件描述符,出错为-1

pathname、mode含义与open相同

creat的不足指出是以只写方式打开所创建的文件,若要读取该文件,必须先调用create和clos然后在调用open

现在可以用下列方式调用open,达到上面的效果

open(pahtname,O_RDWR|O_CREAT|O_TRUNC,mode)

(3)close函数

#include <unistd.h>
int close(int fd)
close函数返回,成功返回0,出错-1

2、文件的定位

当前文件位移量,非负整数,度量一个文件开始处计算的字节数,读写操作从当前文件位移量处开始

调用lseek函数显示的定位一个打开文件

#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd,off_t offset,int whence)
fd表示已打开文件的描述符

offset表示位移量的大小,单位字节

whence,解释参数offset大小,下面是取值和含义

whence取值含义
SEEK_SET将该文件的位移量设置为距文件开始处offset个字节
SEEK_CUR将该文件的位移置量设置为其当前增加offset个字节,offset可为正或负
SEEK_END将该文件的位移量设置为文件长度加offset个字节,offset可为正或负

3、文件的读写

(1)、read函数

用read函数打开文件读取数据

#include <unistd.h>
ssize_t read(int fd,void *buf,size_t count)
返回,读到的字节数,若已到文件尾端返回0,出错返回-1

fd表示进行操作的文件描述符

buf是一个指向缓冲区的指针

count表示本次操作将要读取的数据字节数


(2)、write函数

用write函数向打开文件写数据

#include <unistd.h>
ssize_t write (int fd,void *buf,size_t count)
返回,成功为已写的字节数,出错为-1

fd操作的文件描述符

buf指向缓冲区的指针,放入要写入文件的数据

count表示本次操作写入文件的数据字节数


三、文件的属性操作

1、改变文件访问权限

#include <sys/types.h>
#include <sys/stat.h>
int chmod (const char *pathname,mode_t mode)
int fchmod(int fd,mode_t mode)
chmod,fchmod返回,成功0,出错-1
chmod对指定文件进行操作,fchmod对已经打开的文件进行操作

mode_t可以用16进制的0777来表示,比shell中的chmod 777多个0


2、改变文件所有者

#include <sys/types.h>
#include <unistd.h>
int chown(const char *pathname,uid_t owner gid_t group)
int fchown(int fd,uid_t owner,gid_t group)
int lchown(const char *pathname,uid_t owner,gid_t group)
返回,成功返回0,出错为-1

chown修改文件所有者,pathname绝对路径或相对路径,owner文件所有者标识号,group组标识号

fchown修改已打开文件的所有者,fd文件描述符,owner、group相同

lchown针对符号链接文件,更改链接文件不是链接指向的文件


3、重命名

#include <stdio.h>
int rename (const char *oldname,const char *newname)
返回,成功为0,出错为-1

rename会将oldname所指定的文件名修改为参数newname所指定的文件名

若newname已存在,则会被删除

oldname和newname指向同一个文件时,直接返回成功


4、修改文件长度

#include <sys/types.h>
#include <unistd.h>
int truncate (const char *pathname,off_t len)
int ftruncate(int fd,off_t len)
返回,成功为0,出错为-1


四、其他操作

1、从stat结构提中取回文件信息

linux系统的所有文件都有个与之对应的索引节点,该节点中包含了文件的相关信息,这些信息被保存在stat结构体中

#include <sys/types.h>
#include <sys/stat.h>
int stat(const char *pathname,struct stat * sbuf)
int fstat(int fd,struct stat *sbuf)
int lstat(const char *pathname,struct stat *sbuf)
返回,成功0,出错-1

2、复制现存的文件描述符

#include <unistd.h>
int dup(int fd)
int dup2(int fd,int fd2)
返回,成功为新的文件描述符,出错为-1

dup返回当前可用文件描述符最小数值,dup2可以用fd2指定新描述符的数值

3、fcntl改变已经打开文件的性质

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd,int cmd)
int fcntl(int fd,int cmd,long arg)
没什么用的函数?!

4、sync和fsync

将修改后保存在缓存中的那日哦那个写入磁盘

#include <unistd.h>
void sync(void)
int fsync(int fd)
返回,成功0,出错-1

sync只将修改过得块缓存排入写队列返回,不等带实际I/O操作结束

fsync指引用单个文件,等待I/O结束,然后返回


五、特殊文件操作

1、目录文件操作

mkdir,rmdir

#include <sys/types.h>
#include <sys/stat.h>
int mkdir(const char *pathname,mode_t mode)
#include <unistd.h>
int rmdir(const char *pathname)
mkdir创建目录

rmdir删除空目录

opendirclosedir,readdir

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *pathname)
int closedir(DIR *dp)
struct dirent *readdir (DIR *dp)

opendir打开目录

closedir关闭目录

readdir目录文件的读取



chdir,fchdir,getcwd

chdir,fchidir更改当前工作目录

getcwd获得当前工作目录绝对路径

#include <unistd.h>
int chidir(const char *pathname)
int fchdir (int fd)
char *getcwd(char *buf,size_t size)



2、链接文件操作

(1)硬链接

创建链接link

#include <unistd.h>
int link(const char *pathname1,const char *pathname2)
返回,成功0,出错-1
pathname2存在时返回-1,root可以创建目录的链接

(2)删除链接

#include <unistd.h>
int unlink(const char *pathname)
#include 
  
  
   
   
int remove(const char *pathname)

  
  
返回,成功为0,出错为-1

remove可以删除链接,也可以删除目录

(3)符号连接

#include<unistd.h>
int symlink (const *actualpath,const char *sympath)
返回,成功0,出错-1




3、管道文件操作

管道文件用于不同进程间的数据和信息传递,一个进程将要传递的数据或信息写入管道的一段,另一个进程从管道另一端取得所需数据

#include <stdio.h>
int pipe(int filedes[2])
返回,成功返回0,出错为-1

参数filedes是含有两个元素的数组,调用pipe成功创建管道后,将返回两个文件描述符,分别到管道的两端


4、设备文件

处理/dev下的文件











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值