= 写了一段程序,其中有错误,思考良久,还是没有想通? =

软件和网站开发以及相关技术探讨
头像
nobrain
帖子: 808
注册时间: 2005-08-25 13:58
来自: ustc
联系:

#16

帖子 nobrain » 2007-04-05 21:22

DRIFT 写了:freeflag@freeflag-desktop:~/temp$ gcc test.c -o test -Wall
test.c: In function ‘main’:
test.c:16: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
test.c:21: warning: passing argument 3 of ‘write’ makes integer from pointer without a cast
freeflag@freeflag-desktop:~/temp$
你改成功了吗? &a NOT a
爱喝真猪奶茶的夜鸣猪
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04

#17

帖子 DRIFT » 2007-04-06 20:25

呵呵 ,让大家笑话了.

我突然发现我居然犯了一个如此可笑的错误------连scanf()函数都不会用了.

:oops: :oops: :oops: :oops: :oops:
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04

#18

帖子 DRIFT » 2007-04-06 21:13

DRIFT 写了:

代码: 全选

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

int
main(int argc,char *argv[])
{	int fd,a,b;
	if(argc!=2)
	{	printf("Error-1\n");
		exit(1);
	}
	printf("Input:");
	scanf("%d",&a);
	if((fd=open(argv[1],O_RDWR|O_CREAT))==-1)
	{	printf("Error-2\n");
		exit(2);
	}
	b=write(fd,"123456",a);
	if(b==-1)
	{	printf("Error-3\n");
		exit(3);
	}
	else
	{	printf("%d\n",b);
	}
	if((close(fd))==-1)
	{	printf("Error-4\n");
		exit(4);
	}
	if((execlp("more","more",argv[1],NULL))<0)
	{	printf("Error-5.\n");
		exit(5);
	}
	return 0;
}

总是有错,错误代码"Error-3"

写这个程序只是为了让我更好的理解write()函数的用法.谢谢大家的帮助!
回复