-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.c
74 lines (64 loc) · 1.65 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define DEV_NAME "/dev/globalvar"
#define LOGAL_LEN 4
int main(int argc, char *argv[]) {
int ret; // 返回值
unsigned char buf[10] = "123456789"; // 4字节的变量
unsigned char res[10];
// 1.open
int fd = open(DEV_NAME, O_RDWR);
if (fd < 0) {
printf("\033[31mFail to open file %s\033[0m\n", DEV_NAME);
return -1;
}
// 2.write
ret = write(fd, &buf, 6);
printf("ret : %d\n", ret);
// 3.read
read(fd, &res, 6);
printf("\033[32mresult of read is: %s\033[0m\n", res);
char order = 'c';
int num;
int isCycle = 1;
printf("Please input order, q: exit, r: read, w: write\n");
while (isCycle) {
unsigned char str[10];
scanf("%c", &order);
if (order == ' ' || order == '\n')
continue;
switch (order) {
case 'r': // 读
printf("How number words do you want to read: ");
scanf("%d", &num);
num = num > 0 ? num : LOGAL_LEN;
read(fd, &str, num);
str[num] = '\0';
printf("\033[32mresult of read is: %s\033[0m\n", str);
break;
case 'w': // 写
printf("Please input the word to write: ");
scanf("%s", str);
printf("Try to write: %s\n", str);
char *ptr = str;
num = 0;
while (*ptr != '\n' && *ptr != '\0') {
++ptr;
++num;
}
write(fd, &str, num);
read(fd, &str, LOGAL_LEN);
printf("\033[32mAfter writing, is %s\033[0m\n", str);
break;
case 'q':
isCycle = 0;
break;
default:
printf("\033[31mWrong order: %c\033[0m\n", order);
}
}
close(fd);
return 0;
}