不带缓存的文件I/O操作 open/close

来源:互联网 发布:基德级与现代级 知乎 编辑:程序博客网 时间:2024/05/16 01:50

open.c

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
    int fd;
    if((fd=open("/WIN_UBUNTU/sample/open/opentest.c",O_CREAT|O_TRUNC|O_WRONLY,0600))<0)
    {
        perror("open:");
        exit(1);
    }
    else
    {
        printf("open file: opentest.c %d\n", fd);
    }

    if(close(fd)<0)
    {
        perror("close:");
        exit(1);
    }
    else
    {
        printf("close file: opentest.c %d\n", fd);
    }
    exit(0);
}

Makefile

obj=open.o

open:$(obj)
    gcc $(obj) -o open
open.o:open.c
    gcc -c open.c

clean:
    rm $(obj) open opentest.c
原创粉丝点击