[已解决][问题]ubuntu下的编程问题

软件和网站开发以及相关技术探讨
回复
lovelifestar
帖子: 9
注册时间: 2008-06-05 10:01

[已解决][问题]ubuntu下的编程问题

#1

帖子 lovelifestar » 2008-06-16 13:01

刚刚开始学习linux,所以对这方面不太了解
最近买了一本书unix/linux编程 实践教程,在上面遇到了一个程序,是关于模仿cp命令的
也就是要实现cp的功能,可是老是报段错误

代码: 全选

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

#define BUFFERSIZE 4096
#define COPYMODE 0644

void oops(char *,char *);

main(int ac,char *av)
{
	int in_fd,out_fd,n_chars;
	char buf[BUFFERSIZE];
	
	if(ac!=3){
		fprintf(stderr,"useage:%s source destination\n",*av);
		exit(1);
	}
	if((in_fd=open(av[1],O_RDONLY))==-1){
		oops("Cannot open ",av[1]);
	}
	if((out_fd=creat(av[2],O_RDONLY))==-1){
		oops("Cannot creat ",av[2]);
	}
	while((n_chars = read(in_fd,buf,BUFFERSIZE))>0)
		if(write(out_fd,buf,n_chars)!=n_chars)
		oops("Write error to ",av[2]);
	if(n_chars==-1)
		oops("read error from ",av[1]);
	if(close(in_fd)==-1||close(out_fd)==-1)
		oops("Error closing files","");
}

void oops(char *s1,char *s2){
	fprintf(stderr,"Error: %s",s1);
	perror(s2);
	exit(1);
}

我用的是anjuta编译器,请问这是怎么回事,谢谢[/code]
上次由 lovelifestar 在 2008-06-16 15:40,总共编辑 1 次。
lovelifestar
帖子: 9
注册时间: 2008-06-05 10:01

#2

帖子 lovelifestar » 2008-06-16 15:40

解决了,结贴
将char *av改为char *av[]
purplepower99
帖子: 82
注册时间: 2008-04-14 15:38

#3

帖子 purplepower99 » 2008-06-20 9:13

恭喜楼主啦,哈哈哈哈哈哈哈哈
回复