#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
/*
int on;
int led_no;
int fd;
if (argc != 3 || sscanf(argv[1], "%d", &led_no) != 1 || sscanf(argv[2],"%d", &on) != 1 ||
on < 0 || on > 1 || led_no < 0 || led_no > 3) {
fprintf(stderr, "Usage: leds led_no 0|1/n");
exit(1);
}
printf("%d/n", led_no);
printf("%d/n", on);
*/
int irq;
int irqCount;
char interruptType[10];
char deviceName[10];
char *string = "333: 222 su ha";
sscanf(string, "%d: %d %s %s ", &irq, &irqCount, interruptType, deviceName); printf("%d/n", irq);
printf("%d/n", irqCount);
printf("%s/n", interruptType);
printf("%s/n", deviceName);
}
sscanf的作用是将字符串按照指定的格式输出
比如注释下面的代码,因为string中字符是两个整形后面又接两个字符串,所以sscanf的打印格式要为"%d: %d %s %s ",
又因为打印格式中有:,这就要求string字符串在两个整形之间要有一个:存在。
注释里的代码要求执行时,要加两个整形的参数,比如./sscanf 2 1 (这里主要注意if判断的用法)