1

I am trying to solve this K&R question. I tried this code in CodeBlocks.

int main()
{
    int c, d;
    while ( (c=getchar()) != EOF)
    {
        d = 0;
        if (c == '\\')
        {
            putchar('\\');
            putchar('\\');
            d = 1;
        }
        if (c == '\t')
        {
            putchar('\\');
            putchar('t');
            d = 1;
        }
        if (c == '\b')
        {
            putchar('\\');
            putchar('b');
            d = 1;
        }
        if (d == 0)
            putchar(c);
    }
    return 0;
}

But when i press backspace \b is not being displayed in place of that. enter image description here

Please help me.

Abhishek kumar
  • 2,586
  • 5
  • 32
  • 38

3 Answers3

1

It's because the console window handles keyboard and editing keys itself.

You have to look into the Windows console functions, especially the SetConsoleMode function.


To clear the ENABLE_PROCESSED_INPUT and ENABLE_LINE_INPUT flags:

// Get the console handle for `stdin`
HANDLE hConsoleStdin = GetStdHandle(STD_INPUT_HANDLE);

// Get the current flags
DWORD flags;
if (GetConsoleFlags(hConsoleStdin, &flags))
{
    // Now `flags` contain the current flags
    // Remove the flags we don't want there
    flags &= ~(ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT);

    // And finally set the new flags
    SetConsoleFlags(hConsoleStdin, flags);
}

Note: The above code is not tested, as I don't have access to a Windows machine.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • @Joachim-Please give an insight how console window handles backspace.Thanks – Abhishek kumar Nov 18 '12 at 19:10
  • @Abhishekkumar The console window is a separate program, that runs other programs when requested. It's that program program, when it receives a backspace, modifies the buffer that is sent to your program when the user presses enter. All editing (moving cursor, backspace and delete keys, inserting or overwriting text) is all done in the console program before your program treceives the text. You have tell the console program to not do such editing with the `SetConsoleMode` function. – Some programmer dude Nov 18 '12 at 19:13
  • ENABLE_PROCESSED_INPUT 0x0001 CTRL+C is processed by the system and is not placed in the input buffer. If the input buffer is being read by ReadFile or ReadConsole, other control keys are processed by the system and are not returned in the ReadFile or ReadConsole buffer. If the ENABLE_LINE_INPUT mode is also enabled, backspace, carriage return, and line feed characters are handled by the system. – Abhishek kumar Nov 18 '12 at 19:15
  • @Abhishekkumar You just call it, with the correct flags. See the example in my updated answer. – Some programmer dude Nov 18 '12 at 19:25
0

The problem actually is, your console does not pass the back-space to the program, but deletes the character from the input-buffer. As @JoachimPileborg said, see the use of SetConsoleMode and other functions.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

The details of how to accomplish this in a Linux environment were answered here.

Community
  • 1
  • 1
jpb
  • 356
  • 4
  • 4