文件描述符
0:标准输入
1:标准输出
2:标准错误
系统调用
write:把缓冲区buf的前nbytes个字节写入与文件描述符fildes关联的文件中,返回实际写入的字节数。
#include <unistd.h>
size_t write(int fildes, const void *buf, size_t nbytes);
read:从文件描述符fildes相关的文件里读入nbytes个字节的数据,并把它们放到数据区buf中,返回实际读入的字节数。
#include <unistd.h>
size_t read(int fildes, void *buf, size_t nbytes);
open:创建一条到文件或设备的访问路径,返回一个可以被read,write,和其他系统调用使用的文件描述符。
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
close:终止文件描述符fildes与其对应文件之间的关联,文件描述符被释放并能够重新使用。close调用成功时返回0,出错时返回-1。
#include <unistd.h>
int close(int fildes);
ioctl:提供了一个用于控制设备及其描述符行为和配置底层服务的接口。对描述符fildes引用的对象执行cmd参数中给出的操作。
#include <unistd.h>
int ioctl(int fildes, int cmd, ...);
lseek:对文件描述符fildes的读写指针进行设置,即设置文件的下一个读写位置。返回从文件头到文件指针被设置处的字节偏移量,失败时返回-1。
#include <unistd.h>
#include <sys/types.h>
off_t lseek(int fildes, off_t offset, int whence);
fstat、stat、lstat:返回与打开文件描述符相关的文件的状态信息,该信息将会写到一个buf结构中,buf的地址以参数形式传递给fstat。
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
int fstat(int fildes, struct stat *buf);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
dup、dup2:dup系统调用复制文件描述符fildes,返回一个新的描述符。dup2系统调用则是通过明确指定目标描述符来
#include <unistd.h>
int dup(int fildes);
int dup2(int fildes, int fildes2);