最近使用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; }