[英]Not being able to get backspace character (\b) in the output on ubuntu (K&R example)
#include <stdio.h>
/* replacing tabs and backspaces with visible characters */
int main()
{
int c;
while ( (c = getchar() ) != EOF) {
if ( c == '\t')
printf("\\t");
else if ( c == '\b')
printf("\\b");
else if ( c == '\\')
printf("\\\\");
else
putchar(c);
}
return 0;
}
现在我的问题是..为什么我不能在输出中看到“\\ b”? 我已经在Ubuntu终端中编写了这段代码。 有什么其他方法可以在输出中获得“\\ b”字符吗? 如果有,请用简单的词语解释,因为我刚开始学习C编程。这个例子来自K&R练习1-10。
运行程序并输入Ctrl H.
由退格键(也是:---)发送的密钥代码最有可能被shell吃掉。 这取决于终端的配置方式。 请阅读此处了解详细信息: http : //www.tldp.org/HOWTO/Keyboard-and-Console-HOWTO-5.html
Is there any other way to get the "\b" character in output ?
如果您提供的输入如下:
ab backspace cd
到你的程序然后它会导致abc
因为这是shell传递给程序的。
确保将正确的输入发送到程序 。 通过说:
printf $'ab\bcd' | /path/to/executable
它打印出预期的输出,即:
ab\bcd
这不是有效的C代码。 它应该如下所示:
#include <stdio.h>
/* replacing tabs and backspaces with visible characters */
int main()
{
int c;
while ( (c = getchar() ) != EOF) {
if ( c == '\t')
printf("\\t");
else if ( c == '\b')
printf("\\b");
else if ( c == '\\')
printf("\\\\");
else
putchar(c);
} // <-- note the closing curly brace
return 0;
}
您应该准备一个包含\\b
( 0x08
)的文件,并将其用作程序的输入。 另一种方法是按Ctrl - H然后按Enter键 (感谢@alk为组合键)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.