回 帖 发 新 帖 刷新版面

主题:[讨论]请教下一个简单的linux程序如何改正错误

下面这个liunx程序我能编译通过。。但是在运行的时候出现了段错误。请问下是哪里出了问题了啊?

请大家给我说哈啊。。谢谢了。。。
#include<sys/types.h>
#include<stdio.h>
#include<sys/stat.h>
#include<errno.h>
#include<unistd.h>
#include<fcntl.h>
#define BUFFER_SIZE 1024
                                                                                
int copy(int argc,char **argv)
   {
    int from_fd,to_fd;                                                     
    int bytes_read,bytes_write;
    char buffer[BUFFER_SIZE];
    char *ptr;
    if(argc!=3)
    {
        fprintf(stderr,"Usage:%s fromfiletofile\n\a",argv[0]);
        exit(1);
   }
 if((from_fd=open(argv[1],O_RDONLY))==-1)
    {
      perror("open error!");
  exit(1);
    }
if((to_fd=open(argv[2],O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))==-1)
    {
       perror("open error!");
        exit(1);
    }
while(bytes_read=read(from_fd,buffer,BUFFER_SIZE))
                                                                                
    {
   if((bytes_read==-1)&&(errno!=EINTR)) break;
  else if(bytes_read>0)
   {
        ptr=buffer;
while(bytes_write=write(to_fd,ptr,bytes_read))
{
if((bytes_write==-1)&&(errno!=EINTR))break;
                else if(bytes_write==bytes_read)
                 break;
                else if(bytes_write>0)
                {
                    ptr+=bytes_write;
                    bytes_read-=bytes_write;
}
            }
            if(bytes_write==-1)break;
}
}
                                                                                
}
                                                                                
int sum()
{
int i, ji=1;
for(i=1;i<=50;i++)
{ji=ji*i;}
printf("%d\n",ji);
}
                                                                                
int main(int argc ,char** argv)
{
    int start=time((time_t *)NULL);
    printf("time start:%d\n",start);
copy(argv[1],argv[2]);
//off_t lseek(espipe,0,SEEK_END)
{
open(argv[2],O_RDWR);
sum();
}         

回复列表 (共2个回复)

沙发

copy(argv[1],argv[2]);
//你copy调用不对啊,和你定义的都不一样

板凳

sum应该是算阶乘的吧?
你打算怎么运行这个程序阿
不知道你要干什么

我来回复

您尚未登录,请登录后再回复。点此登录或注册