9

When I read the C Programming Language and do the exercise 1-10, there is a problem made me puzzled.

It's said that when I enter a backspace, the character is processed by the console driver and not delivered to the program, so what I can do is create a file with embedded backspace.However, it's seemed useless no matter I directly enter '\b' or press Ctrl + H.

When I press Ctrl + H, the screen will show "\b", but when I run the program, it seems that the program will still see it as two characters '\' and 'b'. No matter what I enter, it never shows "\backspace" when I run the program.

What should I do to make the program recognize it as a character of backspace?

My codes are like following:

#include <stdio.h>
int main()
{
    int c;
    while((c=getchar())!=EOF){
        if(c=='\t')
            printf("\\t");
        else if(c=='\\')
            printf("\\\\");
        else if(c=='\b')
                 printf("\\backspace");
             else
                 putchar(c);
     }
}
200_success
  • 7,286
  • 1
  • 43
  • 74
Harukaze
  • 2,239
  • 2
  • 10
  • 7
  • "create a file" How did you create this file? (what text editor?) – Rufflewind Jan 19 '16 at 04:08
  • I use vim and create a file named b.Then type the command ./a.out – Harukaze Jan 19 '16 at 04:12
  • 1
    You have to use a [special key combination in vim to enter control characters](http://stackoverflow.com/a/1585463) such as backspace. In your case, you should type `ctrl + v`, followed by `ctrl + h`. That will enter a real backspace character. – Rufflewind Jan 19 '16 at 04:20
  • Oh, no, it's not Ctrl +h. It's other letters and I forgot it.Now, I can't find it and know what those letters are. I'm so sorry – Harukaze Jan 19 '16 at 04:22
  • Well, I press Ctrl+h, and Ctrl + v, and when I press wq, it's print that b written. But when I type the command to run the program, it still can't print \backspace.It seems that the program recognize it as a empty line and print none in that line. – Harukaze Jan 19 '16 at 04:44

3 Answers3

4

I don't think the problem is with your code, but with the way you wrote the backspace character in your text editor.

You have to use a special key combination in vim to enter control characters such as backspace. In your case, you should type ctrl + v followed by ctrl + h. That should produce a real backspace character.

To see whether you have produced an actual backspace character, you can use hexdump:

$ hexdump myfile
00000000  68 65 6c 6c 6f 20 08 77  6f 72 6c 64              |hello .world|
                            ^^

Notice the 08, which is the backspace character (in C, it's denoted \b).


Another way to produce the backspace character is to simply write it via a program in C:

#include <stdio.h>
int main(void) {
    FILE *f = fopen("myfile", "w");
    fputs("hello \bworld", f);
    fclose(f);
    return 0;
}
Community
  • 1
  • 1
Rufflewind
  • 8,545
  • 2
  • 35
  • 55
3

The problem is not really in your program, but, like you said, in your terminal driver. The odd behaviour, as observed by your program, is a consequence of the Unix terminal model.

Notice that after you press Tab, you probably also have to press Enter before your program sees the Tab as a \t character. That means that your terminal is in "cooked" mode (i.e., not raw mode). Similarly, the terminal driver will handle Ctrl + H before your program's getchar() will ever get a chance to see it.

What you can do is run stty -icanon before launching your program. (Alternatively, you can do it programmatically within your program's initialization routine.) Then, keystrokes such as Tab and Ctrl + H keypress will be instantly picked up by getchar(), literally.

To restore the default terminal behaviour, use stty icanon or stty sane.

200_success
  • 7,286
  • 1
  • 43
  • 74
  • I found the man page of stty and find the "stty sane", and I still puzzled that how to use it and what like the correct format. Sorry, I'm a linux newer, and could you tell me what's the correct format command when using stty icanon and stty sane? – Harukaze Jan 19 '16 at 05:26
2

Backspace is ASCII code 8.

So you could check on c == 0x08 when reading from a file.

else if(c == 0x08)
    printf("\\backspace");

You might want to check this.

Community
  • 1
  • 1
Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • Well, I have seen it just now and understanding that program. But, it seems that it's different from mine. I just can't make the "\c" within a file can be recognized as one character when I make the program redirect to a file named b which embedded "\b" – Harukaze Jan 19 '16 at 04:20