1

Possible Duplicate:
how to check for the “backspace” character in C

I just want to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\. However, i use the similar method like replacing each tab by \t cannot replacing each backspace by \b.How can I capture backspace?

  #include<stdio.h>
    main(){
      int c;


      char tab='t';
      char bsps='b';

      int i;
      int j;
      while((c=getchar())!=EOF)
      { 

        if(c == 9)
        { 
            putchar(92);
            putchar(tab);

        } 
        else if(c == 8)
        { 
            putchar(92);
            putchar(bsps);
        } 
        else if(c == 92)
        { 
             putchar(92);
                 putchar(92);
        } 
        else putchar(c);
      }

    }
Community
  • 1
  • 1
CathyLu
  • 717
  • 2
  • 10
  • 16
  • 1
    I guess you were working on the question 1-10 of c programming language. You can use (ctrl + h) to input 'backspace', as wikipedia suggests: https://en.wikipedia.org/wiki/Backspace#.5EH – Sining Liu Aug 14 '18 at 02:34

1 Answers1

1

Your program is correct. The problem is that it never sees the backspace character which is "eaten" be the line editing method of your OS.

You type ABasckspaceBENTER, but your program only gets BENTER.

I compiled your program and tested it with my OS.

First a simple test

$ echo -e 'a\bc' | wc
      1       1       4

This shows 1 newline, 1 word, and 4 bytes.

Then a run of the program

$ echo -e 'a\bc' | ./a.out 
a\bc
pmg
  • 106,608
  • 13
  • 126
  • 198