UNIX环境高级编程Android

从零开始UNIX环境高级编程(3):文件I/O

2017-01-21  本文已影响113人  伤口不该结疤

open 和 openat

函数原型

int open(const char *pathname, int flags);
int openat(int dirfd, const char *pathname, int flags);

open和openat区别

示例代码

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

int main(int argc, char const *argv[])
{
    int fd = -1;

    if (open("./file/test.txt", O_CREAT) == -1)
    {
        printf("cretae file fail\\n");
    }

    if ((fd = open("/Users/zhanghuamao/unix/", O_RDONLY)) == -1)
    {
        err_sys("open dir fail\\n");
    }
    else
    {
        printf("fd = %d\\n", fd);
    }


    if (openat(fd, "./text.txt", O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
    {
        err_sys("openat file fail");

    }

    return 0;
}

运行结果

zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ./open_test 
fd = 4
zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ls -l ./file
total 0
-rwx------  1 zhanghuamao  staff  0  1 22 20:31 test.txt
zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ pwd
/Users/zhanghuamao/unix/code/chapter3
zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ls -l ../..
total 0
drwxr-xr-x  4 zhanghuamao  staff  136  1 21 10:07 code
-rwx------  1 zhanghuamao  staff    0  1 22 20:30 text.txt

注:
由于Mac OS X中/usr/include目录的Operation not permitted问题,无法将apue.h复制到/usr/include,直接将apue.h放到inc目录进行include。

zhanghuamaodeMacBook-Pro:code zhanghuamao$ ls
chapter3    inc
zhanghuamaodeMacBook-Pro:code zhanghuamao$ ls -l inc/
total 24
-rw-r--r--@ 1 zhanghuamao  staff  4649  1 12 21:37 apue.h
-rw-r--r--@ 1 zhanghuamao  staff  2282  1 12 21:36 error.c

lseek

函数原型

off_t lseek(int fd, off_t offset, int whence);

说明

使用lseek写入字符串到文件末尾 - 示例代码

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

int main(int argc, char const *argv[])
{
    int offset = -1;
    int fd = -1;
    char* file_path = "/Users/zhanghuamao/unix/code/chapter3/file/text.txt";

    if ((fd = open(file_path, O_RDWR)) == -1)
    {
        err_sys("open fail");
    }

    //SEEK_END: write from the end of file, SEEK_CUR:write from the begin of file
    offset = lseek(fd, 0, SEEK_END);
    printf("before write, offset = %d\\n", offset);

    if (write(fd, "12345", 5) == -1)
    {
        err_sys("write fail");
    }

    offset = lseek(fd, 0, SEEK_CUR);
    printf("after write, offset = %d\\n", offset);

    return 0;
}

运行结果

zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ./lseek_test;cat ./file/text.txt 
before offset = 0
after write, offset = 5
12345zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ./lseek_test;cat ./file/tex .txt 
before offset = 5
after write, offset = 10
1234512345zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ 

空洞文件 - 示例代码

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


int main(int argc, char const *argv[])
{
    char buf1[] = "abcde";
    char buf2[] = "fghij";


    int offset = -1;
    int fd = -1;

    char* file_path = "/Users/zhanghuamao/unix/code/chapter3/file/holefile.txt";

    if ((fd = creat(file_path, FILE_MODE)) == -1)
    {
        err_sys("create fail");
    }

    if (write(fd, buf1, 5) == -1)
    {
        err_sys("write buf1 fail");
    }

    if (lseek(fd, 1000, SEEK_SET) == -1)
    {
        err_sys("lseek error");
    }

    if (write(fd, buf2, 5) == -1)
    {
        err_sys("write buf2 fail");
    }

    /* code */
    return 0;
}

运行结果

zhanghuamaodeMacBook-Pro:file zhanghuamao$ ls
holefile.txt    text.txt
zhanghuamaodeMacBook-Pro:file zhanghuamao$ od -c holefile.txt 
0000000    a   b   c   d   e  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000020   \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
*
0001740   \0  \0  \0  \0  \0  \0  \0  \0   f   g   h   i   j            
0001755

对标准输入设置偏移量 - 示例代码

#include "../inc/apue.h"

#define BUFFSIZE 4096

int main(int argc, char const *argv[])
{
    char buf[BUFFSIZE];

    if (lseek(STDIN_FILENO, 0, SEEK_CUR) == -1)
    {
        printf("can't seek\\n");
        return 0;
    }
    else
    {
        printf("seek ok\\n");

    }

    while (read(STDIN_FILENO, buf , BUFFSIZE) > 0)
    {
        printf("%s", buf);

    }
    printf("\\n");
    return 0;

}

运行结果

zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ./stdin_lseek < ./file/text.txt 
seek ok
1234512345
zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ./stdin_lseek < ./file/text.txt 
seek ok
34512345

文件共享

内核表示打开文件的3种数据结构

内核表示打开文件的3种数据结构

原子操作

多进程同时操作同一文件导致的错误

进程A覆盖调进程B写入的内容 打开文件时设置设置O_APPEND标志

原子操作

所谓原子操作,指的是多步操作组合成为一步,在整个执行过程中,要么执行完所有操作,要么一步都不执行。例如,写入内容到文件末尾的原子操作,是将移动文件偏移量到末尾和写操作组合成为一步。要么两个操作一起执行,要么都不执行,这样就能避免多进程同时操作同一文件的错误。

dup

复制标准输入的文件描述符

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

int main(int argc, char const *argv[])
{
    int fd = -1;
    char buf[5];
    if ((fd = dup(1)) == -1)
    {
        err_sys("dup fail");
    }
    printf("dup fd = %d\n", fd);

    read(fd, buf, 5);
    printf("buf = %s\n", buf);

    return 0;
}

运行结果

在打印出dup fd = 3后,表示复制成功。然后手动输入"12345",打印成功。

zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ./dup_test 
dup fd = 3
12345
buf = 12345

fcntl

函数原型

int fcntl(int fd, int cmd, ... /* arg */ );

获取文件状态标志 - 实例

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

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

    if (argc != 2)
    {
        err_sys("usage: fcntl_test <descriptor#>");
    }

    if ((val = fcntl(atoi(argv[1]), F_GETFL, 0)) < 0)
    {
        err_sys("fcntl error for fd %d\n", 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("unknow access mode");
    }

    if (val & O_APPEND)
    {
        printf(", append");
    }

    if (val & O_NONBLOCK)
    {
        printf(", nonblocking");
    }

    if (val & O_SYNC)
    {
        printf(", synchronous writes");
    }

    putchar('\n');
    return 0;

}

运行结果

zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ./fcntl_test 0 < /dev/tty
read only
zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ./fcntl_test 1 > temp.txt
zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ cat temp.txt 
write only
zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ./fcntl_test 2 2>> temp.txt
write only, append
zhanghuamaodeMacBook-Pro:chapter3 zhanghuamao$ ./fcntl_test 5 5<> temp.txt
read write

参考

上一篇下一篇

猜你喜欢

热点阅读