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;
    }
  • 相关阅读:
    Unity打包ARCore项目失败,但是其他安卓项目成功
    关于Unity 图片队列存储以及出列导致内存溢出的解决方案
    unity 使用 outline 组件
    7Z解压工具的BUG
    Unity ILRuntime 调用方法一览
    Python 免费插件
    SQL经典面试题及答案
    PL/SQL Developer中文注释乱码的解决办法
    Tomcat并发优化和缓存优化
    在配置hibernate.cfg.xml时需指定使用数据库的方言:
  • 原文地址:https://www.cnblogs.com/yihujiu/p/5520976.html
Copyright © 2011-2022 走看看