write&read&open

程式碼修行者發表於2016-07-10

0標準輸入
1標準輸出
2標準錯誤

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int main(){
        char buffer[128];
        int nread;
        nread =  read(0,buffer,128); //返回讀取到的字串個數
        if(nread == -1){
                char *c = "A read sdf error has occurred\n";
                write(2,c,strlen(c));
        }
        if(write(1,buffer,nread) != nread){
                char *c = "A write error has occurred\n";
                write(2,c,strlen(c));
        }
        return 0;
}

這裡寫圖片描述

open

#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
int main(){
        //open("file.in",O_RDONLY); //readonly
        int out = open("file.out",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
        write(out,"hello",5);
        write(out,"wrold",5);
        exit(0);
}

這裡寫圖片描述

這裡寫圖片描述
這裡寫圖片描述