socket学习笔记——IO口的基本操作(读、写)

写操作
1
#include <stdio.h> 2 #include <stdlib.h> 3 #include <fcntl.h> 4 #include <unistd.h> 5 void error_handling(char* message); 6 7 int main() 8 { 9 int fd; 10 char buf[] = "let's go!"; 11 12 fd = open("1.txt",O_CREAT|O_WRONLY); 13 if(fd == -1) 14 error_handling("open error"); 15 printf("file descriptor:%d ",fd); 16 17 if(write(fd,buf,sizeof(buf)) == -1) 18 error_handling("write error"); 19 20 close(fd); 21 return 0; 22 } 23 24 void error_handling(char* message) 25 { 26 fputs(message,stderr); 27 fputc(' ',stderr); 28 exit(1); 29 }
写操作 
1
#include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <fcntl.h> 5 void error_handling(char* message); 6 7 int main() 8 { 9 int fd; 10 char buf[50]; 11 fd = open("1.txt",O_RDONLY); 12 if(fd == -1) 13 error_handling("open error"); 14 printf("file description;%d ",fd); 15 16 if(read(fd,buf,sizeof(buf)-1)==-1) 17 error_handling("read error"); 18 19 printf("file data:%s ",buf); 20 close(fd); 21 return 0; 22 } 23 24 void error_handling(char* message) 25 { 26 fputs(message,stderr); 27 fputc(' ',stderr); 28 exit(1); 29 }
原文地址:https://www.cnblogs.com/boyiliushui/p/4724564.html