#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define DEVICE "/dev/ISMTestDevice"
int main()
{
int i, fd;
char ch, wbuf[100], rbuf[100];
fd = open(DEVICE, O_RDWR);
if(fd < 0) {
printf("Device file opening error.\n");
exit(1);
}
printf("r = read from device\n w = write to device\n enter command:");
scanf("%c", &ch);
switch(ch)
{
case 'w':
printf("enter data less than 100 characters:");
scanf("%[^\n]", wbuf);
write(fd, wbuf, sizeof(wbuf));
break;
case 'r':
read(fd, rbuf, sizeof(wbuf));
printf("data read from device:%s\n", rbuf);
break;
}
return 0;
}
以上程序编译无任何错误。但是,当我以大小写'w'执行程序时,控件不会出现在“ scanf(”%[^ \ n]“,wbuf);” 语句中,而不是该命令提示符是如下所示。
输出:
[root@localhost char_cdev]# ./a.out
r = read from device
w = write to device
enter command:w
enter data less than 100 characters:[root@localhost char_cdev]#