最近使用linux-c編程linux
學習了下文件的讀寫,若是說一切都是文件的話,linux-c的文件系統能夠被抽象爲這五個函數算法
open,read,write,close,fcntl.編程
是否是一切的計算系統均可以理解爲這幾部分呢。輸入,算法,輸出。函數
附上代碼學習
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> //todo add othor function //open(file,mode,flag),read(fd,buf,size),write(fd,buf,size),close(fd) int main(int argc, char const *argv[]) { int fd,size,check_flag; char mesg[] = "a lot mesg on unix open"; char buffer[80]; char *host_str; check_flag = access("test_open.txt",R_OK); if (check_flag !=0){ fd = open("test_open.txt",O_WRONLY|O_CREAT,S_IRWXU); if(fd < 0){ printf("open & create faild \n"); exit(-1); } size = write(fd,mesg,sizeof(mesg)); close(fd); } fd = open("/etc/hosts",O_RDONLY); if(fd < 0){ printf("open faild \n"); exit(-1); } //read(fd,buffer,sizeof(buffer)); while( (size = read(fd,buffer,sizeof(buffer)) ) > 0){ printf("%s\n", host_str); } close(fd); printf("end\n"); return 0; }