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

软件和网站开发以及相关技术探讨
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04

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

#1

帖子 DRIFT » 2007-03-25 17:23

代码: 全选

#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;
	printf("This is a test.\n");
	if(argc>2||argc<2)
	{	prinft("Error-1\n");
		exit(1);
	}
	if((fd=open(argv[1].O_RDWR|O_CREAT))==-1)
	{	prinft("Error-2\n");
		exit(2);
	}
	if((write(fd,"Hello Linux!\n",16))==-1)
	{	prinft("Error-3\n");
		exit(3);
	}
	if((close(fd))==-1)
	{	prinft("Error-4\n");
		exit(4);
	}
	return 0;
}
结果:
$gcc test.c -test

$./test text
This is a test.

$more text
Hello Linux!
Er
$

问题:
为什么会出现一个"Er"?
头像
laborer
帖子: 1016
注册时间: 2005-10-25 11:15
联系:

#2

帖子 laborer » 2007-03-25 21:33

代码: 全选

if((write(fd,"Hello Linux!\n",16))==-1)
16越界了,应该是13。
hreiser@oakland:~$ killall -9 wife
police@oakland:~$ sudo find / -user hreiser
court@oakland:~$ sudo mv /home/hreiser /jail/
court@oakland:~$ sudo usermod -d /jail/hreiser -s "/usr/sbin/chroot /jail/" hreiser
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#3

帖子 BigSnake.NET » 2007-03-25 22:06

laborer 写了:

代码: 全选

if((write(fd,"Hello Linux!\n",16))==-1)
16越界了,应该是13。
计算\0应该是14吧
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
laborer
帖子: 1016
注册时间: 2005-10-25 11:15
联系:

#4

帖子 laborer » 2007-03-25 22:32

BigSnake.NET 写了:
laborer 写了:

代码: 全选

if((write(fd,"Hello Linux!\n",16))==-1)
16越界了,应该是13。
计算\0应该是14吧
就这个例子来看,他不会想把\0写到一个文本文件里吧。
hreiser@oakland:~$ killall -9 wife
police@oakland:~$ sudo find / -user hreiser
court@oakland:~$ sudo mv /home/hreiser /jail/
court@oakland:~$ sudo usermod -d /jail/hreiser -s "/usr/sbin/chroot /jail/" hreiser
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04

#5

帖子 DRIFT » 2007-03-26 13:01

谢谢高人 。 :wink:
头像
titainium
帖子: 689
注册时间: 2006-12-02 12:25

#6

帖子 titainium » 2007-03-26 15:31

晚上回去试一试。
Titainium
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04

#7

帖子 DRIFT » 2007-03-29 12:58

代码: 全选

#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"
wkt
帖子: 849
注册时间: 2006-09-07 22:51
联系:

#8

帖子 wkt » 2007-03-29 13:39

gcc -Wall /tmp/cc.c
看看是
/tmp/cc.c: In function 'main':
/tmp/cc.c:17: warning: format '%d' expects type 'int *', but argument 2 has type 'int'
//修正
scanf("%d",a); /// scanf("%d",&a);
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04

#9

帖子 DRIFT » 2007-04-01 15:14

wkt 写了:gcc -Wall /tmp/cc.c
看看是
/tmp/cc.c: In function 'main':
/tmp/cc.c:17: warning: format '%d' expects type 'int *', but argument 2 has type 'int'
//修正
scanf("%d",a); /// scanf("%d",&a);
没看懂......
头像
nobrain
帖子: 808
注册时间: 2005-08-25 13:58
来自: ustc
联系:

#10

帖子 nobrain » 2007-04-01 15:32

DRIFT 写了:
wkt 写了:gcc -Wall /tmp/cc.c
看看是
/tmp/cc.c: In function 'main':
/tmp/cc.c:17: warning: format '%d' expects type 'int *', but argument 2 has type 'int'
//修正
scanf("%d",a); /// scanf("%d",&a);
没看懂......
就是scanf里面要从输入得到的量要用地址。
所以,scanf("%d",a)是错的,scanf("%d", &a)是正确的。
爱喝真猪奶茶的夜鸣猪
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04

#11

帖子 DRIFT » 2007-04-01 19:47

声名a变量的时候是不是应该用 size_t呀?
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04

#12

帖子 DRIFT » 2007-04-01 20:40

nobrain 写了:
DRIFT 写了:
wkt 写了:gcc -Wall /tmp/cc.c
看看是
/tmp/cc.c: In function 'main':
/tmp/cc.c:17: warning: format '%d' expects type 'int *', but argument 2 has type 'int'
//修正
scanf("%d",a); /// scanf("%d",&a);
没看懂......
就是scanf里面要从输入得到的量要用地址。
所以,scanf("%d",a)是错的,scanf("%d", &a)是正确的。

修改了,还是出错.

大哥能不能给我一段代码事例一下.
wkt
帖子: 849
注册时间: 2006-09-07 22:51
联系:

#13

帖子 wkt » 2007-04-03 18:35

修改了,还是出错.

大哥能不能给我一段代码事例一下.
这也太假了!!
我都试过了!!


用gcc -Wall 把输出贴出来
贴出运行结果!!
DRIFT
帖子: 177
注册时间: 2007-01-25 13:04

#14

帖子 DRIFT » 2007-04-05 12:43

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$
头像
titainium
帖子: 689
注册时间: 2006-12-02 12:25

#15

帖子 titainium » 2007-04-05 14:07

就是scanf的问题,不要用a,要用&a
Titainium
回复