APUE_chapter3 文件IO

来源:互联网 发布:吴京的地位知乎 编辑:程序博客网 时间:2024/06/10 06:07

#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>// 对POSIX操作系统API的访问功能的头文件名称// open lseek open write closeint main(int argc , char* argv[]){// 3.1 - 3.8    int fd = open (argv[1], O_RDWR | O_APPEND | O_CLOEXEC | O_CREAT); //  open    if (fd  == -1)        printf ("fail to open file:%s \n", argv[1]);    else{        printf ("succeed to open file:%s \n", argv[1]);        // 若成功返回新的文件偏移量,出错返回-1;        off_t curpos = lseek (fd, 0, SEEK_CUR);        char* buf = (char*)malloc(100 * sizeof(char));        // 文件尾返回0,出错返回-1        while (read (fd, buf, 100) > 0){            printf ("succeed to read the file : %s \n", buf);        }        char output[] = "hello world!\n";        if ( write (fd, output, strlen(output)) == -1)            printf ("succeed to write the file:%s \n", argv[1]);        if (!close (fd)) // return 0 when succeed                                      //  close            printf ("succeed to close file:%s \n", argv[1]);        else            printf ("fail to close file:%s \n", argv[1]);    }    return 0;}

#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <unistd.h>// 对POSIX操作系统API的访问功能的头文件名称// fsync fdatasyncint main(int argc , char* argv[]){ // 3.13    int fd = open (argv[1], O_RDWR | O_APPEND | O_CLOEXEC | O_CREAT);    char output[] = "hello world!\n";    time_t start_time = time(NULL);    for (int i = 0; i < 10000000; ++i)        write(fd, output, strlen(output));    printf ("%d\n", time(NULL) - start_time); // 5s    close(fd);    fd = open (argv[1], O_RDWR | O_TRUNC | O_CLOEXEC | O_CREAT);    fdatasync(fd);     for (int i = 0; i < 10000000; ++i)        write(fd, output, strlen(output)); //10s    printf ("%d\n", time(NULL) - start_time);    close(fd);    fd = open (argv[1], O_RDWR | O_TRUNC | O_CLOEXEC | O_CREAT);    fsync(fd);     for (int i = 0; i < 10000000; ++i)        write(fd, output, strlen(output)); // 15s    printf ("%d\n", time(NULL) - start_time);    close(fd);    printf("finished");    return 0;}


#include <fcntl.h>#include <unistd.h>#include <stdio.h>// fctnl函数 : 改变已经打开文件的属性,错误返回 - 1int main(int argc, char* argv[]){ //3.14    int fd = open (argv[1], O_RDWR | O_APPEND | O_CLOEXEC | O_CREAT); // O_SYNC 可以指定    printf( "%d\n", fcntl (fd, F_GETFD, 0)); // return O_CLOEXEC    int file_state_flag = fcntl(fd, F_GETFL, 0);    if ( file_state_flag < 0){        printf ("fctnl error for fd: %s \n", argv[1]);        exit(1);    }    else{        print_file_state_flag(file_state_flag);// 无法设置O_SYNC, 但显示成功设置        if ( fcntl (fd, F_SETFL,  file_state_flag | O_NONBLOCK | O_SYNC) < 0 ){             printf ("fail to set the file state\n");            exit(1);        }        else{ // 成功返回0            print_file_state_flag( fcntl(fd, F_GETFL, 0));        }    }    close (fd);    return 0;}void print_file_state_flag(int file_state_flag){     switch ( file_state_flag & O_ACCMODE){ // O_ACCMODE = 3;            case O_RDONLY:                printf ("read only \n");                break;            case O_WRONLY:                printf("write only \n");                break;            case O_RDWR:                printf("read and write \n");                break;            default:                printf ("unknown access mode\n");    }    if ( file_state_flag  & O_APPEND)        printf ("append\n");    if ( file_state_flag & O_NONBLOCK)        printf ("nonblock\n");    if ( file_state_flag & O_SYNC)        printf ("sync\n");}void change_file_state_flag(int fd, int flag){ // set or clear    int new_flag = fcntl(fd, F_GETFL, 0);    new_flag |= flag;  // set    //new_flag &= ~flag;  //clear    if (fcntl(fd, F_SETFL, new_flag) < 0){        printf ("fail to set the file state flag\n");        exit(1);    }}


其他资料:点击打开链接

0 0
原创粉丝点击