zoukankan      html  css  js  c++  java
  • Linux _文件操作demo

    main1.c

    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        int i;
    
        char buff[] = "hello world
    ";
        write(1, buff, sizeof(buff));
        write(2, buff, sizeof(buff));
    
        return 0;
    }
    

    main2.c

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) 
    {
        char buff[1024];
        int cnt;
    
        cnt = read(0, buff,  sizeof(buff));
        write(1, buff, cnt);
    
        return 0;
    }
    

    main3.c

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    #include <stdlib.h>
    #include <stdio.h>
    
    
    #define FILE1_NAME "shell_program.pdf"
    #define FILE2_NAME "shell_program2.pdf"
    
    int main(void)
    {
        int file1, file2;
        char c;
    
        file1 = open(FILE1_NAME, O_RDONLY);
        if (file1 < 0) {
            printf("open file %s failed
    ", FILE1_NAME);
        }
    
        file2 = open(FILE2_NAME, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
        if (file1 < 0) {
            printf("open file %s failed
    ", FILE2_NAME);
        }
    
        while(read(file1, &c, 1) == 1) {
            write(file2, &c, 1);
        }
    
        return 0;
    }
    

    main4.c

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    #include <stdlib.h>
    #include <stdio.h>
    
    
    #define FILE1_NAME "shell_program.pdf"
    #define FILE2_NAME "shell_program2.pdf"
    
    int main(void)
    {
        int file1, file2;
        //char c;
        char buff[1024];
        int ret;
    
        file1 = open(FILE1_NAME, O_RDONLY);
        if (file1 < 0) {
            printf("open file %s failed
    ", FILE1_NAME);
        }
    
        file2 = open(FILE2_NAME, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
        if (file1 < 0) {
            printf("open file %s failed
    ", FILE2_NAME);
        }
    
        while((ret = read(file1, buff, sizeof(buff))) > 0) {
            write(file2, buff, ret);
        }
    
        return 0;
    }
    

    main5.c

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    #include <stdlib.h>
    #include <stdio.h>
    
    
    #define FILE1_NAME "main5.c"
    #define FILE2_NAME "main5_2.c"
    
    #define SIZE 50
    
    int main(void)
    {
        int file1, file2;
        //char c;
        char buff[1024];
        int ret;
    
        file1 = open(FILE1_NAME, O_RDONLY);
        if (file1 < 0) {
            printf("open file %s failed
    ", FILE1_NAME);
        }
    
        file2 = open(FILE2_NAME, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
        if (file1 < 0) {
            printf("open file %s failed
    ", FILE2_NAME);
        }
    
        lseek(file1, -SIZE, SEEK_END);
    
        while((ret = read(file1, buff, SIZE)) > 0) {
            write(file2, buff, ret);
        }
    
        return 0;
    }
    

    main6.c

    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    #define FILE_NAME   "test.txt"
    
    
    int flock_set(int fd, int type)
    {
        printf("pid=%d into...
    ", getpid());
    
        struct flock flock;
        flock.l_type = type;
        flock.l_whence = SEEK_SET;
        flock.l_start = 0;
        flock.l_len = 0;   
        flock.l_pid = -1;
    
        fcntl(fd, F_GETLK, &flock);
    
        if (flock.l_type != F_UNLCK) {
            if (flock.l_type == F_RDLCK) {
                printf("flock has been set to read lock by %d
    ", flock.l_pid);
            } else if (flock.l_type == F_WRLCK) {
                printf("flock has been set to write lock by %d
    ", flock.l_pid);
            }
        }
    
        flock.l_type = type;
        if (fcntl(fd, F_SETLKW, &flock) < 0) {
            printf("set lock failed!
    ");
            return -1;
        }   
    
        switch (flock.l_type) {
        case F_RDLCK:
            printf("read lock is set by %d
    ", getpid());
            break;
        case F_WRLCK:
            printf("write lock is set by %d
    ", getpid());
            break;
        case F_UNLCK:
            printf("lock is released by %d
    ", getpid());
            break;
        default:
            break;
        }
    
        printf("pid=%d out.
    ", getpid());
        return 0;
    }
    
    int main(void)
    {
        int fd;
    
        fd = open(FILE_NAME, O_RDWR|O_CREAT, 0666);
        if (fd < 0) {
            printf("open file %s failed!
    ", FILE_NAME);
        }
    
        //flock_set(fd, F_WRLCK);
        flock_set(fd, F_RDLCK);
        getchar();
        flock_set(fd, F_UNLCK);
        getchar();
    
        close(fd);
        return 0;
    }
    
  • 相关阅读:
    关于使用wcf架构分布式系统的一点想法
    vs2012 远程调试服务器上iis下的程序
    F#定义方法参数
    产生不重复的随机数
    巧在C#中设置多维动态数组,可以动态增加及删除
    C#时间相减
    css控制图片大小的方法
    常用网络命令
    DEDE SQL 常用语句
    未审核文档发布时间的自动更改方法for DEDEcms v5.3(修正版)
  • 原文地址:https://www.cnblogs.com/Sico2Sico/p/5384224.html
Copyright © 2011-2022 走看看