Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47392
  • 博文数量: 17
  • 博客积分: 74
  • 博客等级: 民兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-07 23:31
文章分类
文章存档

2012年(12)

2011年(5)

我的朋友
最近访客

分类:

2012-01-12 22:00:09

原文地址:第二个程序 copy 作者:doita

    刚才抄了一段代码,一个自己实现copy功能的简易函数,发现对于新手学习最大的障碍是typo,因为对语法和参数的不熟,导致编译的时候会出现各种诡异的问题,原因很简单,我敲错了。就是一个简单的小程序,还不是我自己写的,居然花了40分钟进行最简单的编译通过。编译的错误是最低级的错误。
  1. #include <sys/types.h>
  2.         #include <sys/stat.h>
  3.         #include <fcntl.h>
  4.         #include <unistd.h>
  5.         #include <stdio.h>
  6.           #define PERMS 0666
  7.           #define DUMMY 0
  8.           #define BUFSIZE 1024
  9.          main (int argc,char *argv[])
  10.           {
  11.                 int source_fd,target_fd,num;
  12.                 char iobuffer[BUFSIZE];
  13.                 if(argc!=3)
  14.                 {
  15.                 printf("Usage:Copy Source file Target file\n");
  16.                 return 1;
  17.                 }
  18.                 if ((source_fd=open(*(argv+1),O_RDONLY,DUMMY))==-1)
  19.                 {
  20.                 printf("Source file open error\n");
  21.                 return 2;
  22.                 }
  23.                 if ((target_fd=open(*(argv+2),O_WRONLY|O_CREAT,PERMS))==-1)
  24.                 {
  25.                 printf("Target file open error!\n");
  26.                 return 3;
  27.                 }
  28.                 while((num=read(source_fd,iobuffer,BUFSIZE))>0)
  29.                         if ( write(target_fd,iobuffer,num)!=num)
  30.                                 {
  31.                                 printf("Target file write error!\n");
  32.                                 return 4;
  33.                                 }
  34.                 close(source_fd);
  35.                 close(target_fd);
  36.                 return 0;
  37.                 }

然后自己又复习了下昨天练习的autoconfig,顺便看看参数,结果发现,我的虚拟机居然没有装man........之后,尝试改变下代码的路径,没有通过autoconf。这个autoconf还需要在以后的过程中掌握,这个暂时不是很紧要。慢慢写点代码才是最重要的。


  

阅读(853) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~