Linux下文件编程实验

 这部分不难但很重要 ,涉及到以后的驱动程序。主要有三个知识点系统调用、C语言库函数访问文件和时间编程。

(1)创建文件

 

 
 
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.  
  4. #include <sys/types.h>  
  5. #include <sys/stat.h>  
  6. #include <fcntl.h>  
  7.  
  8. void  create_file(char *filename){  
  9.  
  10. /*创建的文件具有什么样的属性?*/   
  11.     if(creat(filename,0755)<0){  
  12.         printf("create file %s failure!\n",filename);  
  13.         exit(EXIT_FAILURE);  
  14.     }else{  
  15.         printf("create file %s success!\n",filename);  
  16.     }  
  17. }  
  18.  
  19. int main(int argc,char *argv[]){  
  20.     int i;  
  21.     if(argc<2){  
  22.         perror("you haven't input the filename,please try again!\n");  
  23.         exit(EXIT_FAILURE);  
  24.     }  
  25.  
  26.     for(i=1;i<argc;i++){  
  27.         create_file(argv[i]);     
  28.     }  
  29.  
  30.     exit(EXIT_SUCCESS);  

(2)打开文件

 

 
 
  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3.  
  4. #include <sys/types.h> 
  5. #include <sys/stat.h> 
  6. #include <fcntl.h> 
  7.  
  8. int main(int argc ,char *argv[]){ 
  9.     int fd; 
  10.     if(argc<2){ 
  11.         puts("please input the open file pathname!\n"); 
  12.         exit(1); 
  13.     } 
  14.      
  15.     //如果flag参数里有O_CREAT表示,该文件如果不存在,系统则会创建该文件,该文件的权限由第三个参数决定,此处为0755 
  16.     //如果flah参数里没有O_CREAT参数,则第三个参数不起作用.此时,如果要打开的文件不存在,则会报错. 
  17.     //所以fd=open(argv[1],O_RDWR),仅仅只是打开指定文件 
  18.     if((fd=open(argv[1],O_CREAT|O_RDWR,0755))<0){ 
  19.         perror("open file failure!\n"); 
  20.         exit(1); 
  21.     }else
  22.         printf("open file %d  success!\n",fd); 
  23.  
  24.     } 
  25.     close(fd); 
  26.     exit(0); 
  27.      


(3)综合——实现复制文件

 

  
  
  1. #include <sys/types.h> 
  2. #include <sys/stat.h> 
  3. #include <fcntl.h> 
  4. #include <stdio.h> 
  5. #include <errno.h>  
  6.  
  7. #define BUFFER_SIZE 1024  
  8.  
  9. int main(int argc,char **argv)  
  10. {  
  11.  
  12. int from_fd,to_fd;  
  13. int bytes_read,bytes_write;  
  14. char buffer[BUFFER_SIZE];  
  15. char *ptr;  
  16.  
  17. if(argc!=3)  
  18. {  
  19. fprintf(stderr,"Usage:%s fromfile tofile/n/a",argv[0]);  
  20. exit(1);  
  21. }  
  22.  
  23. /* 打开源文件 */  
  24.  
  25. if((from_fd=open(argv[1],O_RDONLY))==-1)  
  26. {  
  27. fprintf(stderr,"Open %s Error:%s/n",argv[1],strerror(errno));  
  28. exit(1);  
  29. }  
  30.  
  31. /* 创建目的文件 */  
  32.  
  33. if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)  
  34. {  
  35. fprintf(stderr,"Open %s Error:%s/n",argv[2],strerror(errno));  
  36. exit(1);  
  37. }  
  38.  
  39. /* 以下代码是一个经典的拷贝文件的代码 */  
  40.  
  41. while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))  
  42. {  
  43. /* 一个致命的错误发生了 */  
  44. if((bytes_read==-1)&&(errno!=EINTR)) break;  
  45. else if(bytes_read>0)  
  46. {  
  47. ptr=buffer;  
  48. while(bytes_write=write(to_fd,ptr,bytes_read))  
  49. {  
  50. /* 一个致命错误发生了 */  
  51. if((bytes_write==-1)&&(errno!=EINTR))break;  
  52. /* 写完了所有读的字节 */  
  53. else if(bytes_write==bytes_read) break;  
  54. /* 只写了一部分,继续写 */  
  55. else if(bytes_write>0)  
  56. {  
  57. ptr+=bytes_write;  
  58. bytes_read-=bytes_write;  
  59. }  
  60. }  
  61. /* 写的时候发生的致命错误 */  
  62. if(bytes_write==-1)break;  
  63.  
  64. }  
  65. }  
  66. close(from_fd);  
  67. close(to_fd);  
  68. exit(0);  
  69. }  

(4)时间编程

   
   
  1. #include <time.h> 
  2. #include <stdio.h> 
  3.  
  4. int main(void){ 
  5.     struct tm *local; 
  6.     time_t t; 
  7.  
  8.     /* 获取日历时间 */ 
  9.     t=time(NULL); 
  10.      
  11.     /* 将日历时间转化为本地时间 */ 
  12.     local=localtime(&t); 
  13.     /*打印当前的小时值*/ 
  14.     printf("Local hour is: %d\n",local->tm_hour); 
  15.      
  16.     /* 将日历时间转化为格林威治时间 */ 
  17.     local=gmtime(&t); 
  18.     printf("UTC hour is: %d\n",local->tm_hour); 
  19.     return 0; 

 

   
   
  1. #include <time.h> 
  2. #include <stdio.h> 
  3.  
  4. int main(void
  5.     struct tm *ptr; 
  6.     time_t lt; 
  7.      
  8.     /*获取日历时间*/ 
  9.     lt=time(NULL); 
  10.      
  11.     /*转化为格林威治时间*/ 
  12.     ptr=gmtime(&lt); 
  13.      
  14.     /*以格林威治时间的字符串方式打印*/ 
  15.     printf(asctime(ptr)); 
  16.      
  17.     /*以本地时间的字符串方式打印*/ 
  18.     printf(ctime(&lt)); 
  19.     return 0; 
   
   
  1. #include <sys/time.h>  
  2. #include <stdio.h> 
  3. #include <stdlib.h>  
  4. #include <math.h> 
  5.  
  6. /* 算法分析 */ 
  7. void function()  
  8. {  
  9.     unsigned int i,j;  
  10.     double y;  
  11.     for(i=0;i<1000;i++)  
  12.         for(j=0;j<1000;j++)  
  13.             y++;  
  14. }  
  15.  
  16. main()  
  17. {  
  18.     struct timeval tpstart,tpend;  
  19.     float timeuse;  
  20.  
  21.     gettimeofday(&tpstart,NULL); // 开始时间 
  22.     function();  
  23.     gettimeofday(&tpend,NULL);   // 结束时间 
  24.  
  25.     /* 计算执行时间 */ 
  26.     timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+  
  27.         tpend.tv_usec-tpstart.tv_usec;  
  28.     timeuse/=1000000;  
  29.  
  30.     printf("Used Time:%f\n",timeuse);  
  31.     exit(0);  
  32. }  




 

 

本文出自 “Mr~钟” 博客,请务必保留此出处http://6386296.blog.51cto.com/6376296/1118466

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值