zoukankan      html  css  js  c++  java
  • 文件I/O的操作实例

    1_open.c:

    #include <sys/types.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int main(int argc,char *argv[])
    {
        int fd;        //文件描述符
        
        /*以读的方式打开一个文件,如果文件不存在,则返回错误*/
        fd = open("test.txt",O_RDONLY);
        if(fd < 0){
            perror("open");
            return -1;
        }
        printf("fd == %d
    ",fd);
    
        return 0;
    }

    2_open.c:

    #include <sys/types.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int main(int argc,char *argv[])
    {
        int fd;        //文件描述符
        
        /*以读的方式打开一个文件,如果文件不存在,则创建它*/
        fd = open("test.txt",O_RDONLY | O_CREAT,0755);
        if(fd < 0){
            perror("open");
            return -1;
        }
        printf("fd == %d
    ",fd);
    
        return 0;
    }

    3_read.c:

    #include <sys/types.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int main(int argc,char *argv[])
    {
        int fd;        //文件描述符
        int ret;
        char buf[100] = {0};
    
        /*读取标准终端 : 0 (STDIN_FILENO)*/
        ret = read(0,buf,sizeof(buf)-1);
        if(ret > 0){
            /*读取成功*/
    
            /*打印到终端*/
            printf("%s
    ",buf);
        }
    
        return 0;
    }

    4_read.c:

    #include <sys/types.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main(int argc,char *argv[])
    {
        int fd;        //文件描述符
        int ret;
        char buf[100] = {0};
    
        /*打开一个文件,以读的方式*/
        fd = open("test.txt",O_RDONLY);
        if(fd < 0){
            perror("open test");
            return -1;
        }
        
        /*把文件指针移动到文件头*/
        lseek(fd,0,SEEK_SET);
    
        /*读取标准终端 : 0 (STDIN_FILENO)*/
        ret = read(fd,buf,sizeof(buf)-1);
        if(ret > 0){
            /*读取成功*/
    
            /*打印到终端*/
            printf("%s
    ",buf);
        }
    
        return 0;
    }

    5_write.c:

    #include <sys/types.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main(int argc,char *argv[])
    {
        int fd;        //文件描述符
        int ret;
        char buf[100] = {0};
    
        /*打开一个文件,以读的方式*/
        fd = open("test.txt",O_RDONLY);
        if(fd < 0){
            perror("open test");
            return -1;
        }
        
        /*把文件指针移动到文件头*/
        lseek(fd,0,SEEK_SET);
    
        /*读取标准终端 : 0 (STDIN_FILENO)*/
        ret = read(fd,buf,sizeof(buf)-1);
        if(ret > 0){
            /*读取成功*/
    
            /*打印到终端*/
            write(1,buf,ret);
        }
    
        return 0;
    }

    6_write_read.c:

    #include <sys/types.h>
    #include <stdio.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    int main(int argc,char *argv[])
    {
        int fdr;        //文件描述符
        int fdw;
        int ret;
        char buf[100] = {0};
    
        /*打开一个文件,以读的方式*/
        fdr = open("test.txt",O_RDONLY);
        if(fdr < 0){
            perror("open test");
            return -1;
        }
        
        /*打开一个文件dest.txt,用于写,如果文件不存在,则创建*/
        fdw = open("dest.txt",O_WRONLY | O_CREAT,0755);
        if(fdw < 0){
            perror("open dest");
            return -1;
        }
    
        /*把文件指针移动到文件头*/
        lseek(fdr,0,SEEK_SET);
        
        /*读取标准终端 : 0 (STDIN_FILENO)*/
        ret = read(fdr,buf,sizeof(buf)-1);
        if(ret > 0){
            /*读取成功*/
    
            /*写入到文件dest.txt*/
            write(fdw,buf,ret);
        }
    
        return 0;
    }
  • 相关阅读:
    后缀自动机/回文自动机/AC自动机/序列自动机----各种自动机(自冻鸡) 题目泛做
    BZOJ 1001 狼抓兔子 (网络流最小割/平面图的对偶图的最短路)
    FFT与多项式、生成函数题目泛做
    BZOJ 2243 SDOI 2011染色
    莫队/分块 题目泛做
    Cogs 12 运输问题2 (有上下界网络流)
    可并堆/左偏树 题目泛做
    TC快速搜索在win10下不可用
    (转)Tomcat调优
    (转)Tomcat文件详解
  • 原文地址:https://www.cnblogs.com/yihujiu/p/5520976.html
Copyright © 2011-2022 走看看