getchar() and '\b'

This is a discussion on getchar() and '\b' within the C Programming forums, part of the General Programming Boards category; This code works with the excepation of the backspace...where do I error? Code: /*copy input to output and replace all ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    getchar() and '\b'

    This code works with the excepation of the backspace...where do I error?

    Code:
    /*copy input to output and replace all tabs with \t, backspaces with \b, and each backslash with \\.*/
    #include <stdio.h>
     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);
                     }
           }
     }

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    It's likely because the terminal from which you are running the program is doing translations, and has code to treat a backspace as the user would expect. For example, if you typed "foo<backspace>", your program would likely read in a 'f' and a 'o', not the expected 'f' 'o' 'o' '\b'. There are platform specific ways for sending a literal backspace. For example on a unix like system, control-v control-h will likely generate a \b character.

    Further, if you were to pass a file containing a \b character into the standard input of the above program, you'd also see a \b.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    2
    Thanks!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21