• C

gotoxy or cursorto


Is there any ANSI C function like GotoXY or CursorTo of Pascal ?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ANSI C does not deal with screens, i'm afraid.

There's a gotoxy() from conio.h.

HTH, julio

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BTW, that applies to DOS... under Unix, you have dive a bit into the curses library:

man curses
andrewyuAuthor Commented:
But, I cannot find this in VC5 !

And I also want to know how to clear the screen ?

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

You want the Win32 console API's .. they are quite extensive

You can clear the screen, fill recatnagles with characters and colours, set text position and colour etc.

Would you like further info??

To clear the screen clrscr(). Did you look for <conio.h> in your help docs?

Anyway, as said this is Dos stuff. If you are willing to deal with windows under Win, feel free to repoen this question.

Please reopen the question so I can post an answer relevant to Win32 .. the console support is quite good under Win32 (95 and NT).

SetConsoleCursorPosition() will position the cursor.
Clearing the screen is not directly supported but there's a workaround described in KB article Q99261.
I've not got VC, so cannot do it myself, but wander WHY nobody cares to give andrewyu a topic to look at in his help file?... fighting for 5 points??

I will never understand this area, guys. Sorry.

julio, I gave him two topics.  One, a function name.  Another, a KB article.  Anything wrong with that?

Ponts?  What points?
My comment was basically directed to RONSLOW... no, there's nothing wrong.
BTW, i thought to be *polite*, whoever my comment was meant to.

I asked if info was required on Win32 because info for DOS only was given.

I will gladly put up the info if it is required.

I am not fighting for points .. just wondering if the answer supplied was OK .. it not is can be rejected and I'll post an answer that addresses WIn32.

CONGUI smaple code .. GUI.C


*       This is a part of the Microsoft Source Code Samples.  
*       Copyright (C) 1993-1997 Microsoft Corporation.
*       All rights reserved.  
*       This source code is only intended as a supplement to  
*       Microsoft Development Tools and/or WinHelp documentation.
*       See these sources for detailed information regarding the  
*       Microsoft samples programs.
// GUI.C
// ================================================================
// This module contains all of the functions that interface to the
// 'graphical' part of this program. This currently only relates to
// the code that brings up the dialog box, and calls the WinHelp
// engine.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <windows.h>
#include "ConGUI.h"
int DoHelp (char *szHelpTopic);
int GetDialogArgs (char ***pargv);
BOOL CenterWindow (HWND hwnd);
BOOL APIENTRY CLDlgProc (HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam);
// Use WINHELP to bring up the applicaiton help file
int DoHelp (char *szHelpTopic)
    WinHelp (GetFocus(), "ConGUI.HLP", HELP_KEY, (DWORD)(LPSTR)szHelpTopic);
    return TRUE;
// Bring up the dialog box, and pass back a 'command line' as was
// specified by the user
int GetDialogArgs (char ***pargv)
    int ret;
    HANDLE hinst;
    HWND hwnd;
    char szFile[80];
    hinst = GetModuleHandle (NULL);
    hwnd = GetFocus();
    ret = DialogBoxParam (hinst, "CL", NULL, CLDlgProc, (LPARAM)pargv);
    if (-1 == ret) {
        ret = GetLastError();
        printf ("Unable to create dialog: %d\n", ret);
        GetModuleFileName (hinst, szFile, sizeof(szFile));
        printf ("hinst = %d\n", hinst);
        printf ("hwnd = %d\n", hwnd);
        printf ("File = %s\n", szFile);
        return FALSE;
    return ret;
// A quick little routine that will center a window on the screen.
// Handy for dialog boxes
BOOL CenterWindow (HWND hwnd)
    RECT    rect;
    int     w, h;
    int     wScreen, hScreen, xNew, yNew;
    HDC     hdc;
    GetWindowRect (hwnd, &rect);
    w = rect.right - rect.left;
    h = rect.bottom - rect.top;
    hdc = GetDC (hwnd);
    wScreen = GetDeviceCaps (hdc, HORZRES);
    hScreen = GetDeviceCaps (hdc, VERTRES);
    ReleaseDC (hwnd, hdc);
    xNew = wScreen/2 - w/2;
    yNew = hScreen/2 - h/2;
    return SetWindowPos (hwnd, NULL, xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
// Create a data structure that will hold the strings for the combo boxes
// we have in our dialog. This just illustrates 'a' way to do this, not
// necessarily the best.
typedef struct tagDlgCtrls {
    int ctrlId;
    int def;
    char str[25];
    char opt[5];
} DlgCtrls;
DlgCtrls dlgctrls[] = {
    { 415, FALSE, "DOS EXE", "..." },
    { 415, FALSE, "Windows 3.0 EXE", "..."  },
    { 415, FALSE, "Windows 3.0 DLL", "..."  },
    { 415, FALSE, "Windows 3.1 EXE", "..."  },
    { 415, FALSE, "Windows 3.1 DLL", "..."  },
    { 415, TRUE,  "Windows NT EXE", "..."  },
    { 415, FALSE, "Windows NT DLL", "..."  },
    { 415, FALSE, "Windows NT Console App", "..."  },
    { 402, FALSE, "Small", "AS" },
    { 402, FALSE, "Medium", "AM" },
    { 402, FALSE, "Compact", "AC" },
    { 402, TRUE,  "Large", "AL" },
    { 402, FALSE, "Huge", "AH" },
    { 402, FALSE, "Customize", "A?" },
    { 404, FALSE, "8086", "G0" },
    { 404, FALSE, "80186", "G1" },
    { 404, TRUE,  "80286", "G2" },
    { 404, FALSE, "80386", "G3" },
    { 404, FALSE, "80486", "G4" },
    { 406, TRUE,  "stdcall", "Gz" },
    { 406, FALSE, "Pascal", "Gc" },
    { 406, FALSE, "C", "Gd" },
    { 408, FALSE, "Level 0", "W0" },
    { 408, FALSE, "Level 1", "W1" },
    { 408, FALSE, "Level 2", "W2" },
    { 408, TRUE,  "Level 3", "W3" },
    { 408, FALSE, "Level 4", "W4" },
    { 411, FALSE, "None", "" },
    { 411, FALSE, "Line Numbers Only", "Zd" },
    { 411, TRUE,  "Full Information", "Zi" },
    { 418, FALSE, "Ansi C", "Za" },
    { 418, TRUE, "MS Extensions", "Ze" },
    { 413, FALSE, "None", "" },
    { 413, TRUE,  "Protect Mode App", "GA" },
    { 413, FALSE, "Protect Mode DLL", "GD" },
    { 0, 0}  // End Of List
    int wmId;
    static char ***pargv;
    static char **argv;
    int i, item, index, iCtrl, argc;
    char *cmd;
    char *cmdline;
    switch (msg) {
        case WM_INITDIALOG:
            // We need to initialize stuff in the dialog box...
            pargv = (char ***)lParam;
            argv = *pargv;
            CenterWindow (hdlg);
            iCtrl = i = 0;
            while (dlgctrls[i].ctrlId) {
                if (dlgctrls[i].ctrlId != iCtrl) { // Starting a new list
                    iCtrl = dlgctrls[i].ctrlId;
                index = SendDlgItemMessage (hdlg, iCtrl, CB_ADDSTRING, 0, (DWORD)(LPSTR)dlgctrls[i].str);
                SendDlgItemMessage (hdlg, iCtrl, CB_SETITEMDATA, index, i);
                if (dlgctrls[i].def) {
                    SendDlgItemMessage (hdlg, dlgctrls[i].ctrlId, CB_SETCURSEL, index, 0);
            return (TRUE);
        case WM_DESTROY:
        case WM_COMMAND:
            wmId = LOWORD(wParam);
            switch (wmId) {
                case T_HELP:
                    DoHelp ("Contents");
                case IDOK:
                    cmd = cmdline = (char *)GlobalAlloc (GPTR, 128);
                    argv[0] = cmdline;
                    argc = 0;
                    if (cmdline) {
                        iCtrl = i = 0;
                        while (dlgctrls[i].ctrlId) {
                            if (dlgctrls[i].ctrlId != iCtrl) {
                                iCtrl = dlgctrls[i].ctrlId;
                                index = SendDlgItemMessage(hdlg, iCtrl, CB_GETCURSEL, 0, 0);
                                if (index) {
                                    item = SendDlgItemMessage (hdlg, iCtrl, CB_GETITEMDATA, index, 0);
                                    wsprintf ((LPSTR)cmd, "-%s", (LPSTR)dlgctrls[item].opt);
                                    cmd += strlen(cmd);
                                    cmd[0] = 0;
                                    argv[++argc] = ++cmd;
                    } // if (cmdline)...
                    EndDialog(hdlg, argc);
                    return (TRUE);
                case IDCANCEL:
                    EndDialog(hdlg, 0);
                    return (TRUE);
    return (FALSE);
    lParam; // unreferenced formal parameter

Oops wrong one ... here is more relevant code from CONSOLE.c in Console app.  This has some useful routines and examples of how to do what you require

* FUNCTION: myPuts(HANDLE hConsole, PCHAR s)
* PURPOSE: write a string to the given console buffer, appending a cr/lf
* INPUT: the console to write to, and the string to write
* RETURNS: TRUE if success, FALSE if an error occured
BOOL myPuts(HANDLE hConsole, PCHAR s) {   BOOL bSuccess;   DWORD cCharsWritten;
  const PCHAR crlf = "\n";   BOOL retflag = TRUE;  
  /* write the string to the console */
  bSuccess = WriteConsole(hConsole, s, strlen(s), &cCharsWritten, NULL);
  retflag = bSuccess;
  /* put a carriage return & line feed after the string */
  bSuccess = WriteConsole(hConsole, crlf, strlen(crlf), &cCharsWritten, NULL);
  if (!bSuccess) retflag = FALSE;

* PURPROSE: to get the current width of the console output buffer
* INPUT: the handle to get the information for
* RETURNS: the width of the current console output buffer, in chars
  GetConsoleScreenBufferInfo(hCon, &csbi);

* PURPROSE: to get the current height of the console output buffer
* INPUT: the handle to get the information for
* RETURNS: the height of the current console output buffer, in chars
  GetConsoleScreenBufferInfo(hCon, &csbi);

* FUNCTION: putStatusLine(HANDLE hOut, PCHAR buf)
* PURPOSE: display a string on the top line of the console output buffer
* INPUT: the output handle, the string to display
* RETURNS: none
void putStatusLine(HANDLE hOut, PCHAR buf) {   BOOL bSuccess;
  /* position the status line at (0, 0) */   const COORD dwWriteCoord = {0, 0};
  DWORD cCharsWritten;
  int len; /* the length of the input string parameter */   CHAR szTemp[256];
  SHORT sWidth; /* console width */    sWidth = getConX(hOut);
  strcpy(szTemp, buf);   len = strlen(szTemp);
  memset(szTemp + len, ' ', sWidth - len);  /* blank out rest of line */
  /* write the string to the console at the correct position */
  WriteConsoleOutputCharacter(hOut, szTemp, sWidth,
      dwWriteCoord, &cCharsWritten);
  /* color the status line so it stands out */
  FillConsoleOutputAttribute(hOut, FOREGROUND_RED |
      BACKGROUND_WHITE, sWidth, dwWriteCoord, &cCharsWritten);

* FUNCTION: cls(HANDLE hConsole)
* PURPOSE: clear the screen by filling it with blanks, then home cursor
* INPUT: the console buffer to clear
* RETURNS: none
void cls(HANDLE hConsole) {
  COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */
  DWORD cCharsWritten;
  CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
  DWORD dwConSize; /* number of character cells in the current buffer */  
  /* get the number of character cells in the current buffer */
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  /* fill the entire screen with blanks */
  FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
      dwConSize, coordScreen, &cCharsWritten);
  /* get the current text attribute */
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  /* now set the buffer's attributes accordingly */
  FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
      dwConSize, coordScreen, &cCharsWritten);
  /* put the cursor at (0, 0) */
  SetConsoleCursorPosition(hConsole, coordScreen);
* FUNCTION: setConTitle
* PURPOSE: simply set the current console title. Called by each demo
*          function to specify the name of the source file that
*          contains the demo function.
* INPUT: null terminated string
void setConTitle(PCHAR szTitle) {   BOOL bSuccess;  
  /* set the console title to the input string parameter */

All for a person who never says "thank you" - or grades you :-)
Well, it is a thank less world out there.
Looking at his info:
>> Last 10 Grades Given
>> C C C C C C C C C C

A fine case of an asshole if I ever saw one.
It seems i'm gonna get the C grade... fine!
BTW, i've recently started my personal "banned users" list - that is, users not to support anymore: three names up to now. Maybe there's a new candidate?
Anyway, let's see what happens before shooting... :)
>> t seems i'm gonna get the C grade... fine!
And yes!  We have a winner!  :-)

Don't worry, he knows how to grade.  Would you like a 'D' instead?

>> C D D C C C C C C C
andrewyuAuthor Commented:
Thank you very much and I have already got my answer !!!!!!
Geez... you must be 12, aren't you?

Good luck, julio
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.