读书人

scull测试时的奇怪有关问题多谢

发布时间: 2012-03-04 11:13:33 作者: rapoo

scull测试时的奇怪问题,谢谢
scull 模块加载后,
测试如下:
[root@localhost myscull]# vi tt
[root@localhost myscull]# cp tt /dev/scull0
cp:是否覆盖“/dev/scull0”? y
[root@localhost myscull]# cat /dev/scull0
This is just a test.
这样结果是对的吧?
不过用下面test.c测试时就出现了错误,不知道是什么原因 ?
结果和测试代码如下,谢谢

[root@localhost myscull]# ./test
Please input a string to write the device
This is just a test.
The value of buf is :This
Read the string from the device...
is ju

================================
test.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <sys/ioctl.h>
6 #include <sys/types.h>
7
8 int main()
9 {
10 /*device path*/
11 char path[]= "/dev/scull0 ";
12 char buf[120],p[120]= "what ";
13 int num;
14 int f = open(path,O_RDWR);
15
16 if(f == -1){
17 printf( "device open error!\n ");
18 return 1;
19 }
20
21 printf( "Please input a string to write the device \n ");
22 scanf( "%s ",buf);
23 printf( "The value of buf is :%s\n ",buf);
24 num = write(f,buf,strlen(buf)+1);
25
26 printf( "Read the string from the device...\n ");
27 read(f,p,num);
28 printf( "%s\n ",p);
29
30 close(f);


31
32 return 0;
33 }


[解决办法]
num = write(f,buf,strlen(buf)+1);

建议打印在这里打印一下num的值。

怀疑是驱动里面write或者read对第3个参数的操作有问题?返回值可能也有问题,中途printk出来,查查看吧。
[解决办法]
scanf( "%s ",buf);

你通过这种方式读 This is just a test.

碰到第一个空格就判定字符串结束,buf的值自然就是This,

如果要读取全部字符串,用gets(buf);

读书人网 >UNIXLINUX

热点推荐