Linux下文件编程简介

对文件操作有两种方式:

 A、系统调用方式,这个是基于具体的操作系统

 B、调用C库函数方式

下面具体讲解这两种方式,以及用到的相关的函数;

A:系统调用(系统函数Linux为例)

综合实例,用系统函数编程实现文件的拷贝:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <sys/types.h>  
  4. #include <sys/stat.h>  
  5. #include <fcntl.h>  
  6.   
  7. #define MAX_SIZE 1024  
  8.   
  9. void copy(char *from_file,char *to_file)  
  10. {  
  11.     int from_fd ;  
  12.     int to_fd;  
  13.     char buff[MAX_SIZE];  
  14.     int read_size;  
  15.     int write_size;  
  16.     if((from_fd = open(from_file,O_RDONLY))==-1)  
  17.     {  
  18.         printf("The file of %s don't exist!\n",from_file);  
  19.         exit(1);  
  20.     }  
  21.     if((to_fd=open(to_file,O_WRONLY|O_CREAT,0777))==-1)  
  22.     {  
  23.         printf("The file of %s don't exist!\n",to_file);  
  24.         exit(1);  
  25.     }  
  26.     while((read_size=read(from_fd,buff,MAX_SIZE))>0)  
  27.     {  
  28.         write_size = write(to_fd,buff,read_size);  
  29.         while(write_size<read_size)  
  30.         {  
  31.             read_size -= write_size;  
  32.             write_size += write(to_fd,buff,read_size);  
  33.         }  
  34.   
  35.     }  
  36.     close(from_fd);  
  37.     close(to_fd);  
  38. }  
  39.   
  40. int main(int argc, char *argv[])  
  41. {  
  42.     int i;  
  43.     char *from_file;  
  44.     char *to_file;  
  45.     if(argc!=3){  
  46.         printf("Please input a right expression\n");  
  47.         exit(1);  
  48.     }else{  
  49.         from_file = argv[1];  
  50.         to_file = argv[2];  
  51.         copy(from_file,to_file);  
  52.     }  
  53.     return 0;  
  54. }  


B,用C库函数编程实现文件的拷贝

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. #define MAX_SIZE 1024  
  6.   
  7. void copy(FILE *form_file,FILE *to_file);  
  8.   
  9. int main(int argc,char *argv[])  
  10. {  
  11.     FILE *from_file;  
  12.     FILE *to_file;  
  13.     if(argc!=3){  
  14.         printf("Please input a right expression!\n");  
  15.         exit(1);  
  16.     }else{  
  17.         if((from_file=fopen(argv[1],"rb"))==NULL)  
  18.         {  
  19.             printf("Can't open the file of %s\n",argv[1]);  
  20.             exit(1);  
  21.         }  
  22.           
  23.         if((to_file=fopen(argv[2],"wb"))==NULL)  
  24.         {  
  25.             printf("Can't open the file of %s!\n",argv[2]);  
  26.             exit(1);  
  27.         }  
  28.           
  29.         copy(from_file,to_file);  
  30.     }  
  31.     return 0;  
  32. }  
  33.   
  34. void copy(FILE *from_file,FILE *to_file)  
  35. {  
  36.     char buff[MAX_SIZE];  
  37.   
  38.     long file_size;  
  39.     fseek(from_file,0L,SEEK_END);  
  40.     file_size = ftell(from_file);  
  41.     fseek(from_file,0L,SEEK_SET);  
  42. //  printf("The file size is:%d\n",file_size);    
  43.     while(!feof(from_file))  
  44.     {  
  45.         memset(buff,0,MAX_SIZE);  
  46.         fread(buff,MAX_SIZE,1,from_file);  
  47.         if(file_size<MAX_SIZE){  
  48.             fwrite(buff,file_size,1,to_file);  
  49.         }else{  
  50.             fwrite(buff,MAX_SIZE,1,to_file);  
  51.             file_size -= MAX_SIZE;   
  52.         }     
  53.     }  
  54.     fclose(from_file);  
  55.     fclose(to_file);  
  56. }  

相关资料链接:http://www.educity.cn/linux/1241676.html

                     http://www.cnblogs.com/TianFang/archive/2013/01/22/2870663.html

                     http://wenku.baidu.com/link?url=l6Y04y286Fs7YlgGAHBSGhqnz-2VdZ0-KWQhLEklcYwyhAu4oAtZXS0npqeovSrnKMEiE22w_k0-HSFmCIM4aH8bmHTf0o7fn9BrvjMRCRm



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值