FlushConsoleInputBuffer

This is a discussion on FlushConsoleInputBuffer within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I have some problems flushing the input buffer of a console using the FlushConsoleInputBuffer function. It just doesn't seem ...

  1. #1
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404

    FlushConsoleInputBuffer

    Hi,

    I have some problems flushing the input buffer of a console using the FlushConsoleInputBuffer function. It just doesn't seem to work, even if the function return success. Maybe my vision of what is an input buffer is wrong (i always thought that if characters aren't return to you by a read function, they are considered still being in the input buffer; at least, that's how it "works" with the standard C functions). So... why isn't it working ? I'm quite lost.

    Here's a simple test program i did, trying to find out what was wrong
    Code:
    #include <windows.h>
    #include <string.h>
    
    #define ERR() MessageBoxA(NULL, "Something went wrong somewhere", "Error", MB_OK)
    
    int WINAPI WinMain(HINSTANCE hI, HINSTANCE hPI, LPSTR lpCL, int nCS)
    {
        HANDLE stdOutputHandle;
        HANDLE stdInputHandle;
        DWORD dw;
        BOOL b;
        char c;
    
        const char *msg1 = "Enter 'q' to quit.";
    
        if (AllocConsole() != 0)
        {
            /* A: Houston, we have a console */
    
            stdOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
            if (stdOutputHandle == INVALID_HANDLE_VALUE)
                ERR();
            stdInputHandle = GetStdHandle(STD_INPUT_HANDLE);
            if (stdInputHandle == INVALID_HANDLE_VALUE)
                ERR();
    
            do
            {
                WriteConsoleA(stdOutputHandle, msg1, strlen(msg1), &dw, NULL);
                if (dw != strlen(msg1))
                    ERR();
    
                ReadConsoleA(stdInputHandle, &c, 1, &dw, NULL);
                if (dw != 1)
                    ERR();
    
                if (FlushConsoleInputBuffer(stdInputHandle) == 0)
                    ERR();
            } while (c != 'q');
    
            FreeConsole();
        }
    
        return 0;
    }
    As for the compiler, I'm using Visual Studio 2005 Pro.

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you'd like to tell us what the error code is from your function, using GetLastError()?

    --
    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
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Well, every function return "success". So there's no error code to show, except if I'm missing something.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    You should learn how to use PCH, since including windows.h everytime you compile is pretty painful
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by foxman View Post
    Well, every function return "success". So there's no error code to show, except if I'm missing something.
    So, what are you then saying is not working as you expect?

    --
    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.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    If you call GetNumberOfConsoleInputEvents before and after FlushConsoleInputBuffer you will see that as it is stated in the description the Flush does remove the InputEvents from buffer

    So, what are you then saying is not working as you expect?
    As I saw it - If you press " \n" when asking to input something

    ReadConsoleA reads one byte, but on the next iteration it continues to read next char, so it seems that input buffer was not cleared as expected (and in the same time GetNumberOfConsoleInputEvents returns 0). It seems to me ReadConsoleA buffers the current input event buffer somehow, so it is already remove from the queue.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Quote Originally Posted by matsp View Post
    So, what are you then saying is not working as you expect?
    Well i was expecting the FlushConsoleInputBuffer function to flush the input buffer, but it's not doing it the way i was thinking.

    Example, if i enter "hello" when ask to enter something, the ReadConsoleFunction will return me the 'h' in the car variable on the first iteration, then 'e' at the second iteration, etc, where i was expecting the FlushConsoleInputBuffer function to remove all this stuff so i could enter something on the next iteration.

    From what i saw from vart explanation, the Flush function does remove something from the input buffer, but what, i don't know lol (i would need to check this out but i don't have the time right now), but i do know it's not removing what i wanted it to remove (the characters).

    Anyway, thanks for the replies everyone. Guess I'll just empty the buffer (or whatever you want to call it) by making repetitive call to ReadConsole until the '\n' character is reached.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,651
    I noticed that FlushConsoleInputBuffer() is a "Low-Level Console Input Function", and ReadConsole() is a "High-Level Console Input Function" - perhaps one doesn't affect the other?

    Would be interesting to see if any of the "High-Level Console Modes" changes this behavior.

    gg

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