《unix环境高级编程》-- 文件I/O

测试能否对标准输入设置偏移量
仅将文件偏移量记录在内核内,不引起任何I/O操作

#include "apue.h"

int main(void)
{
    /*
       off_t lseek(int fileds, off_t offet, int whence)
       为打开的文件设置偏移量
        whence: SEEK_SET SEEK_CUR SEET_END
    */
    if(lseek(STDIN_FILENO, 0, SEEK_CUR) == -1)
        printf("cannot seek\n");
    else
        printf("seek OK\n");
    exit(0);
}

这里写图片描述

创建一个具有空洞的文件

#include "apue.h"
#include <fcntl.h>

char buf1[] = "abcdefghij";
char buf2[] = "ABCDEFGHIJ";

/* #define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) */

int main(void)
{
    int fd;
    if((fd = creat("file.hole", FILE_MODE)) < 0)
        err_sys("create error");

    if(write(fd, buf1, 10) != 10)
        err_sys("buf1 write error");
    /* offset now = 10 */

    if(lseek(fd, 16384, SEEK_SET) == -1)
        err_sys("lseek error");
    /* offset now = 16384 */

    if(write(fd, buf2, 10) != 10)
        err_sys("buf2 write error");
    /* offset now = 16394 */

    exit(0);
}

od 观察文件实际内容,-c 标志以字符方式打印文件内容
这里写图片描述

将标准输入复制到标准输出

#include "apue.h"

#define BUFSIZE 4096

int main(void)
{
    int n;
    char buf[BUFSIZE];

    /* ssize_t read(int fileds, const void *buf, size_t nbytes); */
    while((n = read(STDIN_FILENO, buf, BUFSIZE)) > 0)
    {
        /* ssize_t write(int fileds, const void *buf, size_t nbytes); */
        if(write(STDOUT_FILENO, buf, n) != n)
            err_sys("write error");
    }

    if(n < 0)
        err_sys("read error");

    exit(0);
}

这里写图片描述
当BUFSIZE为4096时,CPU时间最小

这里写图片描述

对于指定的描述符打印文件标志

#include "apue.h"
#include <fcntl.h>

int main(int argc, char *argv[])
{
    int val;

    if(argc != 2)
        err_quit("usage: a.out <descriptor#>");

    /*
       int fcntl(int fileds, int cmd, ...);
       F_GETFL:  获得文件状态标志
    */
    if((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0)
        err_sys("fcntl error for fd %d", atoi(argv[1]));

    switch(val & O_ACCMODE)
    {
        case O_RDONLY:
            printf("read only");
            break;
        case O_WRONLY:
            printf("write only");
            break;
        case O_RDWR:
            printf("read write");
            break;
        default:
            err_dump("unkown access mode");
    }

    if(val & O_APPEND)
        printf(", append");
    if(val & O_NONBLOCK)    
        printf(", nonblocking");
 #if defined(O_SYNC)
    if(val & O_SYNC) /* 等待写完成 */
        printf(", synchronous writed");
 #endif
 /* 功能测试宏 */
 #if !defined(_POSIX_C_SOURCE) && defined(O_FSYNC)
    if(val & O_FSYNC)
        printf(", synchronous writes");
 #endif
    putchar('\n');
    exit(0);
}

这里写图片描述
5<>temp.foo表示在文件描述符5上打开文件temp.foo以供读和写

对一个文件描述符打开一个或多个文件状态标志

#include "apue.h"
#include <fcntl.h>

/*
  flags are file status flags to turn on
*/
void set_fl(int fd, int flags)
{
    int val;

    if((val = fcntl(fd, F_GETFL, 0)) < 0)
        err_sys("fcntl F_GETFL error");

    val |= flags;  /* turn on flags */

    /*
       int fcntl(int fileds, int cmd, ...);
       F_SETFL: 设置文件状态标志
    */
    if(fcntl(fd, F_SETFL, val) < 0)
        err_sys("fcntl F_SETFL error");
}

/*
  flags are file status flags to turn off
*/
void clr_fl(int fd, int flags)
{
    int val;

    if((val = fcntl(fd, F_GETFL, 0)) < 0)
        err_sys("fcntl F_GETFL error");

    val &= ~flags;  /* turn off flags */

    if(fcntl(fd, F_SETFL, val) < 0)
        err_sys("fcntl F_SETFL error");
}

int main(void)
{
    /*
       打开同步标志,每次write都要等待,直到数据已写到磁盘再返回
       通常write只是将数据排入队列,实际的写磁盘操作可能在以后的某个时刻进行
    */
    set_fl(STDOUT_FILENO, O_SYNC);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u012319493/article/details/80342177