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\n",szBuf);
        
        
        read(STDIN_FILENO,szBuf,sizeof(szBuf));
        //printf("input:%s\n",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\n",szBuf);
        
        
        read(STDIN_FILENO,szBuf,sizeof(szBuf));
        //printf("input:%s\n",szBuf);
        write(STDOUT_FILENO,szBuf,strlen(szBuf));

        
        return;
}

3.重定向

void TestDup()
{
    int fd;
    char szBuf[100] = "hello world\n";
    
    //fd指向标准输出
    fd = dup(STDOUT_FILENO);
    
    //写入fd
    write(fd,szBuf,strlen(szBuf));
    
    //
    char szTmp[100]="";
    read(fd,szTmp,100);
    
    printf("fd:%d\n",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\n");
        printf("Hello world1113\n");
        
        char szBuf[] = "Hello Ubuntu\n";
        write(STDOUT_FILENO,szBuf,sizeof(szBuf));
        
        write(fd,"Hello linux\n",12);
        //恢复STDOUT_FILENO指针,printf这时候才写入,把printf中到内容输出到屏幕
        dup2(tmpfd,STDOUT_FILENO);
        printf("I am resume\n");
        return;
        
}

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

void TestDir()
{
    char szBuf[100];
    if(NULL == getcwd(szBuf,100))
    {
            perror("Fail to open");
            return;
    }
    printf("szBuf:%s\n",szBuf);

    //修改工作目录为上一级目录
    chdir("../");
    getcwd(szBuf,100);
    printf("szBuf:%s\n",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\n",szBuf);
 24         
 25         
 26         read(STDIN_FILENO,szBuf,sizeof(szBuf));
 27         //printf("input:%s\n",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\n",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\n";
 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\n",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\n");
101         printf("Hello world1113\n");
102         
103         char szBuf[] = "Hello Ubuntu\n";
104         write(STDOUT_FILENO,szBuf,sizeof(szBuf));
105         
106         write(fd,"Hello linux\n",12);
107         //恢复STDOUT_FILENO指针,printf这时候才写入,把printf中到内容输出到屏幕
108         dup2(tmpfd,STDOUT_FILENO);
109         printf("I am resume\n");
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\n",szBuf);
124 
125     //修改工作目录为上一级目录
126     chdir("../");
127     getcwd(szBuf,100);
128     printf("szBuf:%s\n",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 }

猜你喜欢

转载自www.cnblogs.com/xiaochi/p/8980794.html
今日推荐