主题:[讨论]请教下一个简单的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();
}
请大家给我说哈啊。。谢谢了。。。
#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();
}