• 8.底层文件库


    1.open write

    void Test_open_demo()
    {
            int fd;
            //fd = open("1.txt",O_RDONLY,0660);
            //写一个文件,如果不存在则进行创建,如果有同名到则创建失败
            fd = open("2.txt",O_WRONLY | O_CREAT | O_EXCL,0660);
            if(fd < 0)
            {
                perror("Fait to open");
                return;
            }
            char szBuf[100]="";
            
            read(fd,szBuf,sizeof(szBuf));
            close(fd);
            
            printf("%s
    ",szBuf);
            
            
            read(STDIN_FILENO,szBuf,sizeof(szBuf));
            //printf("input:%s
    ",szBuf);
            write(STDOUT_FILENO,szBuf,strlen(szBuf));
    
            
            return;
    }

    2.打开设备文件,输出到控制台

    void Test_open_demo()
    {
            int fd;
            //fd = open("1.txt",O_RDONLY,0660);
            //写一个文件,如果不存在则进行创建,如果有同名到则创建失败
            fd = open("2.txt",O_WRONLY | O_CREAT | O_EXCL,0660);
            if(fd < 0)
            {
                perror("Fait to open");
                return;
            }
            char szBuf[100]="";
            
            read(fd,szBuf,sizeof(szBuf));
            close(fd);
            
            printf("%s
    ",szBuf);
            
            
            read(STDIN_FILENO,szBuf,sizeof(szBuf));
            //printf("input:%s
    ",szBuf);
            write(STDOUT_FILENO,szBuf,strlen(szBuf));
    
            
            return;
    }

    3.重定向

    void TestDup()
    {
        int fd;
        char szBuf[100] = "hello world
    ";
        
        //fd指向标准输出
        fd = dup(STDOUT_FILENO);
        
        //写入fd
        write(fd,szBuf,strlen(szBuf));
        
        //
        char szTmp[100]="";
        read(fd,szTmp,100);
        
        printf("fd:%d
    ",fd);
        
        close(fd);
        
        printf("%s",szTmp);
        return;
    }

    4.stdout重定向到1.txt

    void TestDup2()
    {
            int fd;
            
            fd = open("1.txt",O_WRONLY);
            
            if(fd<0)
            {
                    perror("fail to open");
                    return;
            }
            
            //保留STDOUT_FILENO指针
            int tmpfd = dup(STDOUT_FILENO);
            
            //STDOUT_FILENO重定向到fd
            dup2(fd,STDOUT_FILENO);
            
            //到后面到printf一起处理
            printf("12345
    ");
            printf("Hello world1113
    ");
            
            char szBuf[] = "Hello Ubuntu
    ";
            write(STDOUT_FILENO,szBuf,sizeof(szBuf));
            
            write(fd,"Hello linux
    ",12);
            //恢复STDOUT_FILENO指针,printf这时候才写入,把printf中到内容输出到屏幕
            dup2(tmpfd,STDOUT_FILENO);
            printf("I am resume
    ");
            return;
            
    }

    5.获取程序目录以及修改当前工作目录

    void TestDir()
    {
        char szBuf[100];
        if(NULL == getcwd(szBuf,100))
        {
                perror("Fail to open");
                return;
        }
        printf("szBuf:%s
    ",szBuf);
    
        //修改工作目录为上一级目录
        chdir("../");
        getcwd(szBuf,100);
        printf("szBuf:%s
    ",szBuf);
        return;
    }

    完整代码

      1 #include <stdio.h>
      2 #include <fcntl.h>
      3 #include <unistd.h>
      4 #include <string.h>
      5 
      6 //open write
      7 void Test_open_demo()
      8 {
      9         int fd;
     10         //fd = open("1.txt",O_RDONLY,0660);
     11         //写一个文件,如果不存在则进行创建,如果有同名到则创建失败
     12         fd = open("2.txt",O_WRONLY | O_CREAT | O_EXCL,0660);
     13         if(fd < 0)
     14         {
     15             perror("Fait to open");
     16             return;
     17         }
     18         char szBuf[100]="";
     19         
     20         read(fd,szBuf,sizeof(szBuf));
     21         close(fd);
     22         
     23         printf("%s
    ",szBuf);
     24         
     25         
     26         read(STDIN_FILENO,szBuf,sizeof(szBuf));
     27         //printf("input:%s
    ",szBuf);
     28         write(STDOUT_FILENO,szBuf,strlen(szBuf));
     29 
     30         
     31         return;
     32 }
     33 
     34 //打开设备文件,输出到控制台
     35 void TestTty()
     36 {
     37         int fd;
     38         
     39         fd = open("/dev/pts/24",O_WRONLY);
     40         if(fd<0)
     41         {
     42             perror("Fail to open");
     43             return;
     44         }
     45         
     46         char szBuf[100];
     47         int i=0;
     48         while(i++<10)
     49         {
     50                 sprintf(szBuf,"This is line %d
    ",i);
     51                 write(fd,szBuf,strlen(szBuf));
     52         }
     53         close(fd);
     54 }
     55 
     56 //重定向
     57 void TestDup()
     58 {
     59     int fd;
     60     char szBuf[100] = "hello world
    ";
     61     
     62     //fd指向标准输出
     63     fd = dup(STDOUT_FILENO);
     64     
     65     //写入fd
     66     write(fd,szBuf,strlen(szBuf));
     67     
     68     //
     69     char szTmp[100]="";
     70     read(fd,szTmp,100);
     71     
     72     printf("fd:%d
    ",fd);
     73     
     74     close(fd);
     75     
     76     printf("%s",szTmp);
     77     return;
     78 }
     79 
     80 //stdout重定向到1.txt
     81 void TestDup2()
     82 {
     83         int fd;
     84         
     85         fd = open("1.txt",O_WRONLY);
     86         
     87         if(fd<0)
     88         {
     89                 perror("fail to open");
     90                 return;
     91         }
     92         
     93         //保留STDOUT_FILENO指针
     94         int tmpfd = dup(STDOUT_FILENO);
     95         
     96         //STDOUT_FILENO重定向到fd
     97         dup2(fd,STDOUT_FILENO);
     98         
     99         //到后面到printf一起处理
    100         printf("12345
    ");
    101         printf("Hello world1113
    ");
    102         
    103         char szBuf[] = "Hello Ubuntu
    ";
    104         write(STDOUT_FILENO,szBuf,sizeof(szBuf));
    105         
    106         write(fd,"Hello linux
    ",12);
    107         //恢复STDOUT_FILENO指针,printf这时候才写入,把printf中到内容输出到屏幕
    108         dup2(tmpfd,STDOUT_FILENO);
    109         printf("I am resume
    ");
    110         return;
    111         
    112 }
    113 
    114 //获取程序目录以及修改当前工作目录
    115 void TestDir()
    116 {
    117     char szBuf[100];
    118     if(NULL == getcwd(szBuf,100))
    119     {
    120             perror("Fail to open");
    121             return;
    122     }
    123     printf("szBuf:%s
    ",szBuf);
    124 
    125     //修改工作目录为上一级目录
    126     chdir("../");
    127     getcwd(szBuf,100);
    128     printf("szBuf:%s
    ",szBuf);
    129     return;
    130 }
    131 
    132 int main()
    133 {
    134         //Test_open_demo();
    135         //TestTty();
    136         //TestDup();
    137         //TestDup2();
    138         TestDir();
    139         return 0;
    140 }
  • 相关阅读:
    SQL逻辑查询处理阶段
    将json字符串转换为json兑现
    JSTL核心标签库
    eclipse用4个空格代替Tab 每行80字符限制提示线显示空格
    MyEclipse8.6 性能优化
    jsp/servlet总结复习
    SQLServer之MERGE INTO
    as3效率提升
    让默认TextField更清晰地显示中文
    as3垃圾回收机制
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8980794.html
Copyright © 2020-2023  润新知