Apue Note chapter3

Function:
#include <fcntl.h>
int open(const char *path, int oflag, … /* mode_t mode */);
int openat(int fd, const char *path, int oflag, … /* mode_t mode */);


#include <fcntl.h>
int creat(const char *path, mode_t mode);
等效于:open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);


#include <unistd.h>
int close(int fd);
当一个进程终止时,内核自动关闭它所有打开的文件。很多程序都利用了这一功能而不显示地close关闭打开的文件。


#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);返回值:若成功,返回新的文件偏移量;若出错,返回-1
若whence是SEEK_SET,则将该文件的偏移量设置为距文件开始处offset个字节。
若whence是SEEK_CUR,则将该文件的偏移量设置为当前值加offset,offset可为正或负。
若whence是SEEK_END,则将该文件的偏移量设置为文件长度加offset,offset可为正或负。


实例:
3-1
#include "apue.h"
int main(void)
{
    if(lseek(STDIN_FILENO, 0, SEEK_CUR) == -1)
        printf("can't seek\n");
    else
        printf("seek OK\n");


    exit(0);
}

3-2
#include "apue.h"
#include <fcntl.h>


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

int
main(void)
{
    int fd;

    if ((fd = creat("file.hole", FILE_MODE)) < 0)
        err_sys("creat error");

    if (write(fd, buf1, 10) != 10) {
        err_sys("buf1 write error");
    }

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

    if (write(fd, buf2, 10) != 10)
        err_sys("buf2 write error");

    exit(0);
}


Function
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t nbytes);
返回读到的字节数,若已到文件尾,返回0;若出错,返回-1


ssize_t write(int fd, const void *buf, size_t nbytes)
成功则返回已写入的字节数;出错返回-1


instance 3-5:
#include "apue.h"

#define BUFFSIZE 4096

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

    while((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
        if(write(STDINOUT_FILENO, buf, n) != n)
            err_sys("write error");

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


    exit(0);
}


Function:
#include <unistd.h>
ssize_t pread(int fd, void *buf, size_t nbytes, off_t offset);
相当于lseek后调用read,但这个是原子操作

ssize_t pwrite(int fd, const void *buf, size_t nbytes, off_t offset);

复制现有的文件描述符 file discriptor
int dup(int fd);


int dup2(int fd, int fd2);
若fd2不等于fd, 且fd2已经打开,则先关闭fd2,再dup fd


int fsync(int fd);
int fdatasync(int fd);
void sync(void);

#include <fcntl.h>
int fcntl(int fd, int cmd, ... /* int arg */);


Instance:
3-11
#include "apue.h"
#include <fcntl.h>
int
main(int argc, char *argv[])
{
    int   val;

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

    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("unknown access mode");
    }

    if(val & O_APPEND)
        printf(", append");
    if(val & O_NONBLOCK)
        printf(", nonblocking");
    if(val & O_SYNC)
        printf(", synchronous writes");


#if !defined(_POSIX_C_SOURCE) && defined(O_FSYNC) && (O_FSYNC != O_SYNC)
    if(val & O_FSYNC)
        printf(", synchronous writes");
#endif

    putchar('\n');
    exit(0);
}


测试例子:
$./a.out 0 < /dev/tty   
$./a.out 1 > temp.foo
$./a.out 2 2>>temp.foo
$./a.out 5 5<>temp.foo
function:
#include <unistd.h>  /* System V */
#include <sys/ioctl.h> /* BSD and Linux */
int ioctl(int fd, int request, ...);



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值