c语言 :write与read系统调用总结

在学习的时候总是有点迷糊于是在网上找到一些比较好的分享一下

 

 

 

 

 

 下面举一个例子:

 

 1 #include <stdio.h>  
 2 #include <unistd.h>  
 3 #include <fcntl.h>  
 4 #include <sys/types.h>  
 5 #include <sys/stat.h> 
 6 
 7 const char *pathName = "out.txt";       
 8 int main(void)   
 9 {   
10     int in, out, flag;   
11     char buffer[1024];  
12     in = open("in.txt", O_RDONLY,S_IRUSR);  
13     if (-1 == in) // 打开文件失败,则异常返回  
14     {      
15         return -1;   
16     }     
17     out = open(pathName, O_WRONLY|O_CREAT);  
18     if (-1 == out) // 创建文件失败,则异常返回  
19     {     
20         return -1;   
21     }     
22     while ((flag = read(in, buffer, 1024)) > 0)  
23     {     
24         write(out, buffer, flag);  
25     }     
26     close(in);  
27     close(out);    
28     return 0;  
29 }   

 

 

 

 

 

 

 

 

 

 

 

posted @ 2020-10-18 00:21  lhyvip  阅读(276)  评论(0编辑  收藏  举报