Linux服务器编程——Linux系统编程(2)之I/O函数

C库I/O函数

Linux系统IO函数

open函数
头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

函数原型
int open(const char* pathname,int flags)
pathname:文件路径
flags打开方式:
        必选项O_RDONLY   以只读方式打开
              O_WRONLY   以只读方式打开
              O_RDWR     以读写方式打开
        可选项O_CREAT    创建文件,必须指定mode
              O_EXCL     与O_CREATE同时使用,判断文件是否存在
              O_TRUNC    将文件截断为0

int open(const char * pathname,int flags,mode_t mode)
mode:访问权限

int create(const char* pathname,int flags)

close函数
头文件
#include <unistd.h>

函数原型
int close(int fd)
fd:文件描述符

read函数
头文件
#include <unistd.h>

函数原型
ssize_t read(int fd, void* buf, size_t count)
fd:文件描述符
buf:数据缓冲区
count:读取大小

返回值
-1:失败
0:读完结束
>0:读取的字节数

write函数
头文件
#include <unistd.h>

函数原型
ssize_t write(int fd,void* buf , size_t count)
fd:文件描述符
buf:数据缓冲区
count:字节


lseek函数
头文件
#include <sys/types.h>
#include <unistd.h>

函数原型
off_t lseek(int fd, off_t offset, int whence)
fd:文件描述符
offset:文件指针位置
        SEEK_SET   文件开始位置
        SEEK_CUR   当前
        SEEK_END   结束
whence:偏移量

Linux文件操作相关函数

stat函数  
作用:获取文件属性信息
头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

函数原型
int stat(const char* path,struct stat *buf) -------穿透函数(相对于)软连接
path:文件路径
buf:文件路径
struct stat{
     dev_t     st_dev;    //文件设备编号
     ino_t     st_ino;    //节点
     mode_t    st_mode;     //文件的类型及存取权限
     nlink_t   st_nlink;    //硬链接数目
     uid_t     st_uid     //用户ID
     gid_t     st_gid    //组ID
     dev_t     st_rdev    //(设备类型),若为设备文件,则为设备编号
     off_t     st_size   // 文件字节数
     blksize_t st_blKsize //  块大小
     blkcnt_t  st_blkcnt  //  快数
     time_t    st_atime   //  最后一次访问时间
     time_t    st_mtime   //  最后一次修改时间
     time_t    st_mtime  //   最后一次更改属性时间
};

int fstat(int fd, struct stat *buf)
int lstat(const char* path, struct stat *buf) ----------不穿透函数


access函数
作用:测试指定文件是否拥有某种权限
头文件
#include <unistd.h>

函数原型:
int access(const char* path, int mode)
path:文件名
mode:权限
      R_OK     读权限
      W_OK     写权限
      X_OK     执行权限
      F_OK     是否存在

chmod函数
作用:更改文件权限
头文件
#include <sys/stat.h>

函数原型:
int chmod(const char* path, mode_t mode)
int fchmod(int fd, mode_t mode)

strtol/strtoll函数
头文件
#include <stdlib>

函数原型
long int strtol(const char* nptr,char ** endptr, int base)
long long strtoll(const char* nptr,char ** endptr, int base)
chown函数
头文件
#include <unistd.h>

函数原型
int chown(const char* path,uid_t ower,gid_t group)
int fchown(int fd,uid_t ower,gid_t group)
int lchown(const char* path,uid_t ower,gid_t group)

truncate函数
作用:将参数path指定的文件大小改为length指定的大小

头文件

函数原型
int truncate(const char* path, off_t length)


link函数
作用:创建一个硬链接
头文件
#include <unistd.h>

函数原型
int link(const char* old, char* new)

symlink函数
作用:创建一个硬链接
头文件
#include <unistd.h>

函数原型
int symlink(const char* old, char* new)

readlink函数
作用:读软连接的文件名
头文件

函数原型
ssize_t resdlink(const char* path, char* buf, size_t size)

unlink函数
作用:删除链接数(软链接或硬链接),可用来创建临时文件

头文件
#include <unistd.h>

函数原型
int unlink(const char *pathname)

rename函数

头文件
#include <stdio.h>

函数原型
int rename(const char* old, const char* new)

目录操作函数

chdir函数
作用:修改当前进程路径   cd
头文件
#include <unistd.h>

函数原型
int chdir(const char* path) 
int fchdir(int fd)

getcwd函数
作用:获得当前目录
头文件
#include <unistd.h> 

函数模型
char* getcwd(char* buf,size_t size)

mkdir函数
作用:创建的目录必须有执行权限,否则无法进入
头文件
#include <unistd.h> 
函数模型
int mkdir(const char* path, mode_t mode)

rmdir函数
作用:删除空目录
头文件
#include <unistd.h> 
函数模型
int rmdir(const char* path)

opendir函数
作用:打开一个目录
头文件
#include <sys/types.h>
#include <dirent.h> 
函数模型
DIR *opendir(const char* path) 
返回值
NULL:失败
非NULL:成功

readdir函数
作用:读目录
头文件
函数模型
struct dirent *readdir(DIR* dirp)
返回值:
       struct dirent
       {
            ino_t d_ino;     //目录iNode
            off_t d_off;     //目录文件开头到进入点的位移
            signed short int d_reclen;     //d-name长度
            unsigned char d_type;          //文件类型
                          DT_BLK         块设备
                          DT_CHR         字符设备
                          DT_DIR         目录
                          DT_LNK         软连接
                          DT_FIFO        管道
                          DT_REG         普通文件
                          DT_SOCK        套接字
                          DT_UNKNOWN     未知
                          D_BSD_SOURCE   编译时添加宏定义
            char d_name[256]               //文件名
       }

closedir函数
作用:关闭函数
头文件
函数模型

dup/dup2函数
作用:复制文件描述符
头文件
#include <unistd,h>

#define _GNU_SOURCE
#include <fcntl.h>
#include <unistd.h>
函数原型
int dup(int old)
返回值:未被占用的最小文件描述符

int dup2(int old, int new)
注意:如果new是被打开的,在拷贝之前先关掉new
      如果new和old是同一个fd,则直接返回fd

int dup3(int old, int new , int flags)

fcntl函数
作用:改变已经打开的文件的属性
头文件
#include <fcntl.h>
函数原型
int fcntl(int fd,int cmd)
int fcntl(int fd, int cmd, long arg)
int fcntl(int fd, int cmd , struct flock *lock)

功能描述:
复制一个现有的描述符   cmd   F_DUPFD
获得/设置文件描述符标记     cmd    F_GETFD   F_SETFD
获得/设置文件状态标记      cmd     
                           F_GETFL:
                                   O_RDONLY   只读
                                   O_WRONLY   只写
                                   O_RDWR     读写
                                   O_EXEC     执行
                                   O_SEARCH   搜索打开目录
                                   O_APPEND   追加写
                                   O_NONBLOCK 非阻塞模式
                           F_SETFL:可更改的几个标识
                                   O_APPEND
                                   O_NONBLOCK





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值