Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

An API for getch()...

Does Windows have an API as an equivalent for the C getch()? I am trying to iterate through a data file having over a hundred records. The application works but only the last record is shown on the corresponding text boxes. I need to stop as each record is read and then the next one read and printed on a key being pressed. Have done it in C using getch() but I cant seem to find the requisite API for Visual C++. Any help will be appreciated.
«1

Comments

  • Ed HallEd Hall Posts: 395Member
    Well, I hesitated all day because my thoughts weren't quite along the getch() line, but no one else has weighed in and you did say any help...:-)

    The first thing that comes to mind is a MessageBox(), but that would stick an object in your viewing field, unless you made the MessageBox your vehicle for the information itself. This doesn't seem feasible for more than a small amount of data.

    Next would be to build your own version of getch() with an infinite loop that includes a break statement based on an event. But that would be very inefficient in a windows program.

    That leaves the thought of simply building a routine that steps through the records based on an event, such as a keystroke, etc. Maybe something like:

    [code]
    case IDC_NEXT:
    DisplayRec[rec];
    rec++;
    break;
    [/code]

    As a final thought, perhaps you could display the data fields in a dialog which upon return gathers the next record and reinvokes itself.

    Hope this was of some help, maybe even in sparking a totally separate idea...

    Take Care,
    Ed
  • toddlerasimtoddlerasim Posts: 93Member
    : Well, I hesitated all day because my thoughts weren't quite along
    : the getch() line, but no one else has weighed in and you did say
    : any help...:-)
    :
    : The first thing that comes to mind is a MessageBox(), but that would
    : stick an object in your viewing field, unless you made the
    : MessageBox your vehicle for the information itself. This doesn't
    : seem feasible for more than a small amount of data.
    :
    : Next would be to build your own version of getch() with an infinite
    : loop that includes a break statement based on an event. But that
    : would be very inefficient in a windows program.
    :
    : That leaves the thought of simply building a routine that steps
    : through the records based on an event, such as a keystroke, etc.
    : Maybe something like:
    :
    : [code]:
    : case IDC_NEXT:
    : DisplayRec[rec];
    : rec++;
    : break;
    : [/code]:
    :
    : As a final thought, perhaps you could display the data fields in a
    : dialog which upon return gathers the next record and reinvokes
    : itself.
    :
    : Hope this was of some help, maybe even in sparking a totally
    : separate idea...
    :
    : Take Care,
    : Ed
    :
    :This is my code for the routine:

    case IDC_READ1:

    FILE *p;
    p=fopen("d:\vc6.txt","r");
    while(fread(&teacher, sizeof(teacher),1,p)==1)
    {
    SetDlgItemText(hDlg,IDC_EDIT2,teacher.name);
    SetDlgItemInt(hDlg,IDC_AGE2,teacher.age,FALSE);


    getch(); ======> This is the point where I want the iteration to start.

    }
    fclose(p);

    return TRUE;
    break;

    I am using a Dialog Box with the requisite Text fields to display the output.




  • Ed HallEd Hall Posts: 395Member
    You might want to check out ReadConsoleInput():

    http://msdn.microsoft.com/en-us/library/ms684961(VS.85).aspx

    (I tried to make the above an active link, but PH didn't link it correctly, at least not in preview. Sorry, you'll have to cut and paste.)

    ReadConsoleInput() appears to allow for things like key events etc.

    Sorry I couldn't be of more help.

    Take Care,
    Ed
  • toddlerasimtoddlerasim Posts: 93Member
    : You might want to check out ReadConsoleInput():
    :
    : http://msdn.microsoft.com/en-us/library/ms684961(VS.85).aspx
    :
    : (I tried to make the above an active link, but PH didn't link it
    : correctly, at least not in preview. Sorry, you'll have to cut and
    : paste.)
    :
    : ReadConsoleInput() appears to allow for things like key events etc.
    :
    : Sorry I couldn't be of more help.
    :
    : Take Care,
    : Ed
    :
    :Couldn't be of help?? You don't know what you did for me right now with that link. I was checking it out when I stumbled upon all the API's equivalent to C run time functions. It is amazing. In case you are interested here is the link for it.

    http://support.microsoft.com/kb/99456.

    Like you I can't manage to get the working link here too.

    Thanks a pile for your help. Let me check out and see what the ReadConsoleInput does for me.
  • Ed HallEd Hall Posts: 395Member
    Great list! According to what I see, the function I suggested should work for _getch, but isn't recommended for getch. I wonder what the difference is. Also, in looking down the list, I see _kbhit, which I thought for moment might be along the line of what you wanted, but the PeekConsoleInput returns immediately, while the ReadConsoleInput waits:

    "The function does not return until at least one input record has been read."

    I might play with this myself a little later...

    BTW, the only trouble with your link is the extraneous period at the end. Perhaps the link won't work correctly until the post is submitted.

    The following is a test of a couple versions of the peceding link:

    [link=http://support.microsoft.com/kb/99456]Win32 Equivalents for C Run-Time Functions[/link]

    [link]http://support.microsoft.com/kb/99456[/link]

    http://support.microsoft.com/kb/99456

    The three above links should all go to the same page. In preview the first one is correct, but the second one wants to bring me back to this message, while the third one also points to the correct page.

    For clarity of what I used above, the first is the link=http method in the Style Guide. The second is the link method without the = and the third is just the url.

    If the above links alter from the preview description, I will make note here:

    Take Care,
    Ed
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : [link=http://support.microsoft.com/kb/99456]Win32 Equivalents for C
    : Run-Time Functions[/link]
    :
    : [link]http://support.microsoft.com/kb/99456[/link]
    :
    : http://support.microsoft.com/kb/99456
    :
    : The three above links should all go to the same page. In preview
    : the first one is correct, but the second one wants to bring me back
    : to this message, while the third one also points to the correct page.

    Actually that second link (using the link without = method) points to the first post, and not to mdsn :(

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • toddlerasimtoddlerasim Posts: 93Member
    : : [link=http://support.microsoft.com/kb/99456]Win32 Equivalents for C
    : : Run-Time Functions[/link]
    : :
    : : [link]http://support.microsoft.com/kb/99456[/link]
    : :
    : : http://support.microsoft.com/kb/99456
    : :
    : : The three above links should all go to the same page. In preview
    : : the first one is correct, but the second one wants to bring me back
    : : to this message, while the third one also points to the correct page.
    :
    : Actually that second link (using the link without = method) points
    : to the first post, and not to mdsn :(
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    :The ReadConsoleInput function is returning a failure value of zero and is simply ignored in the while loop. I am still where I was. What a tragedy indeed.

    Come in Lundin AsmGuru62 et al. Where are all the brains hiding? :(
  • LundinLundin Posts: 3,711Member
    I swear I have posted this before but I can't find it...

    Anyway, you already have the answer, PeekConsoleInput().

    For your convenience, here's some quick & dirty functions without any error control. I wrote em ages ago, so please don't sue me if they aren't working... :-)

    [code]/* ConsoleFunctions.h */

    #ifndef __WINAPI_CONSOLE_WRAPPER_H
    #define __WINAPI_CONSOLE_WRAPPER_H

    void init_console_functions (void);

    void gotoxy (int x, int y);

    void clrscr (void);

    char getch (void);


    #endif /* __WINAPI_CONSOLE_WRAPPER_H */[/code]

    [code]/* ConsoleFunctions.c */

    #include "ConsoleFunctions.h"
    #include

    static HANDLE hStdout;
    static HANDLE hStdin;
    static CONSOLE_SCREEN_BUFFER_INFO csbi;
    static const COORD startCoords = {0,0};


    void init_console_functions (void)
    {
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    hStdin = GetStdHandle(STD_INPUT_HANDLE);

    GetConsoleScreenBufferInfo(hStdout, &csbi);
    }

    void gotoxy(int x, int y)
    {
    COORD coord;

    coord.X = x;
    coord.Y = y;

    SetConsoleCursorPosition(hStdout,coord);
    }


    void clrscr(void)
    {
    DWORD dummy;

    FillConsoleOutputCharacter(hStdout,
    ' ',
    csbi.dwSize.X * csbi.dwSize.Y,
    startCoords,
    &dummy);
    gotoxy(0,0);
    }


    char getch(void)
    {
    INPUT_RECORD inp_rec;
    DWORD bytes_read;
    BOOL success;

    do
    {
    success = PeekConsoleInput(hStdin, &inp_rec, 1, &bytes_read);
    FlushConsoleInputBuffer(hStdin);
    } while(!success || inp_rec.EventType != KEY_EVENT || bytes_read==0);

    return (char)inp_rec.Event.KeyEvent.uChar.AsciiChar;
    }[/code]
  • toddlerasimtoddlerasim Posts: 93Member
    : I swear I have posted this before but I can't find it...
    :
    : Anyway, you already have the answer, PeekConsoleInput().
    :
    : For your convenience, here's some quick & dirty functions without
    : any error control. I wrote em ages ago, so please don't sue me if
    : they aren't working... :-)
    :
    : [code]: /* ConsoleFunctions.h */
    :
    : #ifndef __WINAPI_CONSOLE_WRAPPER_H
    : #define __WINAPI_CONSOLE_WRAPPER_H
    :
    : void init_console_functions (void);
    :
    : void gotoxy (int x, int y);
    :
    : void clrscr (void);
    :
    : char getch (void);
    :
    :
    : #endif /* __WINAPI_CONSOLE_WRAPPER_H */[/code]:
    :
    : [code]: /* ConsoleFunctions.c */
    :
    : #include "ConsoleFunctions.h"
    : #include
    :
    : static HANDLE hStdout;
    : static HANDLE hStdin;
    : static CONSOLE_SCREEN_BUFFER_INFO csbi;
    : static const COORD startCoords = {0,0};
    :
    :
    : void init_console_functions (void)
    : {
    : hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    : hStdin = GetStdHandle(STD_INPUT_HANDLE);
    :
    : GetConsoleScreenBufferInfo(hStdout, &csbi);
    : }
    :
    : void gotoxy(int x, int y)
    : {
    : COORD coord;
    :
    : coord.X = x;
    : coord.Y = y;
    :
    : SetConsoleCursorPosition(hStdout,coord);
    : }
    :
    :
    : void clrscr(void)
    : {
    : DWORD dummy;
    :
    : FillConsoleOutputCharacter(hStdout,
    : ' ',
    : csbi.dwSize.X * csbi.dwSize.Y,
    : startCoords,
    : &dummy);
    : gotoxy(0,0);
    : }
    :
    :
    : char getch(void)
    : {
    : INPUT_RECORD inp_rec;
    : DWORD bytes_read;
    : BOOL success;
    :
    : do
    : {
    : success = PeekConsoleInput(hStdin, &inp_rec, 1, &bytes_read);
    : FlushConsoleInputBuffer(hStdin);
    : } while(!success || inp_rec.EventType != KEY_EVENT || bytes_read==0);
    :
    : return (char)inp_rec.Event.KeyEvent.uChar.AsciiChar;
    : }[/code]:

    :Thanks a pile for the info Lundin but I think I did not make my problem clear in my previous post. I read the first record from my file and then want the application to halt till a key is pressed and then the second record to be read and displayed in the Dialog Box. And so on. The actual key pressed is not material. It is the event that matters. Very much like what the _getch() function does. Hope you can come across with some idea as to how this can be done. I don't plan to move on till I have crossed this problem from my list.
  • LundinLundin Posts: 3,711Member
    Can't you simply catch WM_KEYDOWN or similar then?
«1
Sign In or Register to comment.