现在的位置: 首页 > 综合 > 正文

《Linux程序设计第四版》第三章文件操作 读书笔记(三)

2019年07月18日 ⁄ 综合 ⁄ 共 3132字 ⁄ 字号 评论关闭

/** 底层文件访问 */
(1)write系统调用
#include<unistd.h>
size_t write(int fildes,const void *buf,size_t nbytes);
作用:将缓冲区buf中的前nbytes个字节写入与文件描述符fildes关联的文件中。
返回值:返回实际写入的字节数,返回0表示未写入任何数据,返回-1表示在write调用中出现了错误,错误代码保存在errno中。
实例:
#include<stdio.h>
#include<stdlib.h>

int main()
{
    if(write(1,"Here is some data\n",18)!=18)
    {
        write(2,"A write error has occured on file descriptor 1\n",46);
    }

    exit(0);
}
实验过程中发现:
a)对于缓冲区中的字节统计,空格和特殊转移字符均算作一个字节
b)缓冲区"Here is some data\n"和"A write error has occured on file descriptor 1\n"在存储器中是线性存储的,当前者中的字节数不足18时,后者的内容会被写进文件描述符1所对应的文件中。

(2)read系统调用
#include<unistd.h>
size_t read(int fildes,const void *buf,size_t nbytes);
作用:将从文件描述符fildes关联的文件中读入nbytes个字节的数据到buf缓冲区。
返回值:返回实际读入的字节数,返回0表示未读入任何数据,返回-1表示在read调用中出现了错误,错误代码保存在errno中。
实例:
#include<stdio.h>
#include<stdlib.h>

int main()
{
    char buffer[128];
    int nread;

    nread = read(0,buffer,128);
    if(-1 == nread)
    {
        write(2,"A read error has occured\n",26);
    }
    if(write(1,buffer,nread) != nread)
    {
        write(2,"A write error has occured\n,27");
    }
    exit(0);
}

(3)open系统调用
#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);
作用:准备打开的文件或者设备的名字作为参数path传递给函数,oflags参数用于指定打开文件所采取的动作。
     oflags:
         a)O_RDONLY
         b)O_WRONLY
         c)O_RDWR
         可附加的选项,按位或的方式田间
         d)O_APPEND 写入的数据追加的文件的末尾
:         e)O_TRUNC  把文件的长度设置为零,丢弃已有的内容
         f)O_CREAT  如果需要按照mode访问模式创建文件
         g)O_EXCL   与O_EXCL配合使用
    详参man 2 open
    mode设置创建的文件的访问权限,open调用中给出的mode值将于当时的用户掩码的反值AND操作。
    用户掩码umask由3个八进制的数组成,其二进制表示对应位为1的位置表示相应的权限禁止。
返回值:返回一个可以被read、write和其他系统调用使用的文件描述符,且该文件描述符是唯一的,不与其他运行中的进程共享。

(4)close系统调用
#include<unistd.h>
int close()int fildes);
作用:终止文件描述符fildes与其对应文件之间的关联,文件描述符被释放
返回:调用成功返回0,调用失败返回-1

(5)ioct1系统调用
#include<unistd.h>
int ioct1(int fildes,int cmd,...);
作用:对描述符fildes引用的对象执行cmd参数给出的操作。
返回:系统调用成功返回0

(6)lseek系统调用
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
作用:对文件描述符fildes的读写指针进行设置,设置文件的下一个读写位置。offset参数用来指定位置,
      whence指定地址的类型:
         SEEK_SET:绝对地址,SEEK_CUR相对于当前位置的一个相对位置,SEEK_END相对于文件尾的一个相对位置
返回:从文件头到文件指针被设置出的字节偏移值,失败则返回-1.

(7)fstat、stat、lstat系统调用

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

fstat返回与打开的文件描述符相关的文件的状态信息,状态信息写到buf结构中,buf的地址以参数的形式传递给stat
stat和lstat根据文件名查找文件的状态信息,状态信息写到buf结构中,lstat和stat的差异在对于符号链接的处理上,lstat返回链接本身的信息,stat返回该链接指向的文件的信息。

struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };

抱歉!评论已关闭.