免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1375 | 回复: 2
打印 上一主题 下一主题

新手求助 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-07-07 20:24 |只看该作者 |倒序浏览
哪位前辈能帮我看看,为什么我写的两个文件复制的程序,复制结果总是有错误呢?

#include<stdio.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/stat.h>

#define readmsg 200

int main(int argc,char *argv[])
{
  int srcfd,aimfd,length;
  int readlength=0;
  int location;
  char buf[readmsg];
  if(argc!=3)
  {
    printf("Less of parament!\n");
    printf("Please input the command as follow form!\n");
    printf("ProgramName+SourceFileName+AimFileName.\n");
    exit(0);
  }
  if((srcfd=open(argv[1],O_RDONLY))==-1)
  {
    printf("Fail to open SourceFile!\n");
    exit(1);
  }
if((aimfd=open(argv[2],O_RDWR|O_CREAT,0333))==-1)
  {
    printf("Fail to open AimFile!\n");
    exit(1);
  }
  length=lseek(srcfd,0,SEEK_END);
  printf("The lenth of SourceFile is %d bytes!\n",length);
  location=lseek(srcfd,0,SEEK_SET);
  while(length>readmsg)
    {
      if(readlength=(read(srcfd,buf,readmsg))==-1)
      {
        printf("Read SourceFile failure!\n");
        exit(1);
      }
      if((write(aimfd,buf,readmsg))==-1)
      {
        printf("Write to AimFile failure!\n");
        exit(1);
      }
      location=lseek(srcfd,readmsg,SEEK_CUR);
      length=length-readmsg;
    }
  printf("Location is %d from the head of the file!\n");
  printf("Now the VAR length is %d!\n",length);
  read(srcfd,buf,length);
  printf("The messages read from the source file are\n%s\n",buf);
  write(aimfd,buf,length);
  close(srcfd);
  close(aimfd);
  exit(0);
}

论坛徽章:
0
2 [报告]
发表于 2012-07-08 02:26 |只看该作者
  1. #include<stdio.h>
  2. #include<sys/types.h>
  3. #include<fcntl.h>
  4. #include<unistd.h>
  5. #include<stdlib.h>
  6. #include<sys/stat.h>

  7. #define readmsg 200

  8. int main(int argc,char *argv[])
  9. {
  10.   int srcfd,aimfd,length;
  11.   int readlength=0;
  12.   int location;
  13.   char buf[readmsg];
  14.   if(argc!=3)
  15.   {
  16.     printf("Less of parament!\n");
  17.     printf("Please input the command as follow form!\n");
  18.     printf("ProgramName+SourceFileName+AimFileName.\n");
  19.     exit(0);
  20.   }
  21.   if((srcfd=open(argv[1],O_RDONLY))==-1)
  22.   {
  23.     printf("Fail to open SourceFile!\n");
  24.     exit(1);
  25.   }
  26. if((aimfd=open(argv[2],O_RDWR|O_CREAT,0333))==-1)
  27.   {
  28.     printf("Fail to open AimFile!\n");
  29.     exit(1);
  30.   }
  31.   length=lseek(srcfd,0,SEEK_END);
  32.   printf("The lenth of SourceFile is %d bytes!\n",length);
  33.   location=lseek(srcfd,0,SEEK_SET);
  34.   while(length>readmsg)
  35.     {
  36.       if(readlength=(read(srcfd,buf,readmsg))==-1)
  37.       {
  38.         printf("Read SourceFile failure!\n");
  39.         exit(1);
  40.       }
  41.       if((write(aimfd,buf,readmsg))==-1)
  42.       {
  43.         printf("Write to AimFile failure!\n");
  44.         exit(1);
  45.       }
  46.   //    location=lseek(srcfd,readmsg,SEEK_CUR);
  47.       length=length-readmsg;
  48.     }
  49.   printf("Location is %d from the head of the file!\n");
  50.   printf("Now the VAR length is %d!\n",length);
  51.   read(srcfd,buf,length);
  52.   printf("The messages read from the source file are\n%s\n",buf);
  53.   write(aimfd,buf,length);
  54.   close(srcfd);
  55.   close(aimfd);
  56.   exit(0);
  57. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-07-08 13:57 |只看该作者
十分感谢前辈。您的回复犹如醍醐灌顶啊!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP