分页: 1 / 2
= 写了一段程序,其中有错误,思考良久,还是没有想通? =
发表于 : 2007-03-25 17:23
由 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;
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"?
发表于 : 2007-03-25 21:33
由 laborer
代码: 全选
if((write(fd,"Hello Linux!\n",16))==-1)
16越界了,应该是13。
发表于 : 2007-03-25 22:06
由 BigSnake.NET
laborer 写了:代码: 全选
if((write(fd,"Hello Linux!\n",16))==-1)
16越界了,应该是13。
计算\0应该是14吧
发表于 : 2007-03-25 22:32
由 laborer
BigSnake.NET 写了:laborer 写了:代码: 全选
if((write(fd,"Hello Linux!\n",16))==-1)
16越界了,应该是13。
计算\0应该是14吧
就这个例子来看,他不会想把\0写到一个文本文件里吧。
发表于 : 2007-03-26 13:01
由 DRIFT
谢谢高人 。
发表于 : 2007-03-26 15:31
由 titainium
晚上回去试一试。
发表于 : 2007-03-29 12:58
由 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"
发表于 : 2007-03-29 13:39
由 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);
发表于 : 2007-04-01 15:14
由 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);
没看懂......
发表于 : 2007-04-01 15:32
由 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)是正确的。
发表于 : 2007-04-01 19:47
由 DRIFT
声名a变量的时候是不是应该用 size_t呀?
发表于 : 2007-04-01 20:40
由 DRIFT
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)是正确的。
修改了,还是出错.
大哥能不能给我一段代码事例一下.
发表于 : 2007-04-03 18:35
由 wkt
修改了,还是出错.
大哥能不能给我一段代码事例一下.
这也太假了!!
我都试过了!!
用gcc -Wall 把输出贴出来
贴出运行结果!!
发表于 : 2007-04-05 12:43
由 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$
发表于 : 2007-04-05 14:07
由 titainium
就是scanf的问题,不要用a,要用&a