Can someone try getchar() please?

This is a discussion on Can someone try getchar() please? within the C Programming forums, part of the General Programming Boards category; I am doing the exercises in "The C Programming Language" The following program works, except for the backspace '\b'. Can ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Can someone try getchar() please?

    I am doing the exercises in "The C Programming Language" The following program works, except for the backspace '\b'. Can someone help me to get this to work. Using the shell in windows xp to run the program. I tried using the del key ascii code also but it does not work.
    Please ignore the use of the if statements, it's temporary. for some reason the console simply does not recognize \b backspace.
    #include <stdio.h>
    Code:
    main()
    {
        int c, d, bs;
    
        bs = 0;
    
        while((c = getchar()) != EOF)
        {
            d = 0;
            if(c == '\t')
            {
                putchar('\\');
                putchar('t');
                d = 1;
            }
            if(c == '\b')
            {
                putchar('\\');
                putchar('b');
                d = 1;
    	    bs++;
            }
            if(c == '\\')
            {
                putchar('\\');
                putchar('\\');
                d = 1;
            }
    
    
    	if(d == 0)
            {
                putchar(c);
            }
        }
    printf("Backspace: %d", bs);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The only way to read a backspace ('\b') from the console is if you are reading "raw" characters. This is quite complicated and unportable, so you would need to tell us what OS and/or compiler you are using before there is a possibility to help on that.

    In the normal, "cooked" mode, any backspace is processed by the console input processing, and only once the complete line of text is terminated by a newline is the resulting input characters fed to the application.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    2

    OK!

    Thanks for the quick response. I just wanted to make sure I was not doing something wrong as far as the language is concerned. The OS is Windows XP and the compiler is gcc (mingw). If someone could solve this great, I would still be interested in seing it work.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by znum View Post
    Thanks for the quick response. I just wanted to make sure I was not doing something wrong as far as the language is concerned. The OS is Windows XP and the compiler is gcc (mingw). If someone could solve this great, I would still be interested in seing it work.
    The function in the third "box" is for Windows. It's in the FAQ "How do I read input without having to hit Enter" here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 01:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-05-2004, 12:06 AM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM

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