[英]Scanf the null value (ASCII)
我想问一下如何扫描一个值为0x00的字母(我知道它为空)。 我称呼我的函数echo -e'\\ x00'| 函数/.myfunction,我需要将它扫描到一个字符串中,以便以后进行fprint处理。
我该如何扫描一个值为0x00的字母
当然,输入Null字符 1 输入 。 "%s"
与其他任何非空白字符一样对待空字符。
char s[2];
scanf("%1s", s);
将很好地使s[0] == '\\0'
由于输入空字符 )和s[1] == '\\0'
由于所附空字符 。
但是,后续代码可能无法将s[]
作为字符串使用 。 您可能需要使用"%c"
或getchar()
的新方法。 建议一个带有伴随长度的字符数组,而不是字符串 。
如果需要使用scanf()
,则使用"%n"
跟踪长度。
char buf[100];
int n1,n2
if (scanf(" %n%99s%n2", &n1, buf, &n2) == 1) {
int length_read = n2 - n1;
for (int i=0; i<length_read; i++) {
if (isprint((unsigned char) buf[i])) {
printf("%c", buf[i]);
} else {
// do something special for non-printable characters.
printf("(%X)", (unsigned char) buf[i]);
}
}
}
1 空字符可能很难输入,除了OP正在使用的管道之外。 一些键盘允许Crtl Shift @ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.