簡體   English   中英

無法在ubuntu的輸出中獲得退格符(\\ b)(K&R示例)

[英]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;
}

您應該准備一個包含\\b0x08 )的文件,並將其用作程序的輸入。 另一種方法是按Ctrl - H然后按Enter鍵 (感謝@alk為組合鍵)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM