Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2

    Database management question about gotoxy


    whats an alternative to gotoxy() ?

    I'm learning from a book that is a bit old, from 98 and XP days when I think turboC used to work...and maybe gotoxy() used to work in that time. Now times have changed and so in order to understand the sample program given in the book, I need to write an alternative for gotoxy().

    I've looked at SetConsoleCursorPosition() but don't know how it works...

    somebody help me !

    Thanks.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    in Linux gcc compiler gotoxy can be implemented by using printf dont know about other.

    Code:
    void gotoxy(int x,int y)
    {
        printf("%c[%d;%df",0x1B,y,x);
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    Originally Posted by eramit2010
    in Linux gcc compiler gotoxy can be implemented by using printf dont know about other.

    Code:
    void gotoxy(int x,int y)
    {
        printf("%c[%d;%df",0x1B,y,x);
    }
    I'm using codeblocks...I'll try this, maybe it works :confused:
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    No I don't think it works...

    maybe its because u have a semi colon and a bracket placed in the middle of the thing, or maybe its supposed to be like that...I'm new to programming(been like that for quite some time now :D ), so keep that in mind...
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    its working in gcc compiler.
    and ; and bracket are in string so there is not any problem.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    Originally Posted by eramit2010
    its working in gcc compiler.
    and ; and bracket are in string so there is not any problem.
    I'm using codeblocks on windows 7 64-bit, which I think uses GCC right...

    here is the program, try it on ur compiler and see if it works:

    Code:
    /* A menu-driven program for elementary database management */
    #include "stdio.h"
    #include "windows.h"
    main( )
    
    {
    FILE *fp, *ft ;
    char another, choice ;
    struct emp
    {
    char name[40] ;
    int age ;
    float bs ;
    } ;
    struct emp e ;
    char empname[40] ;
    long int recsize ;
    fp = fopen ( "EMP.DAT", "rb+" ) ;
    if ( fp == NULL )
    {
    fp = fopen ( "EMP.DAT", "wb+" ) ;
    if ( fp == NULL )
    {
    puts ( "Cannot open file" ) ;
    }
    }
    recsize = sizeof ( e ) ;
    while ( 1 )
    {
    system("cls") ;
    gotoxy(30,10) ;
    printf ( "1. Add Records" ) ;
    gotoxy ( 30, 12 ) ;
    printf ( "2. List Records" ) ;
    gotoxy ( 30, 14 ) ;
    printf ( "3. Modify Records" ) ;
    gotoxy ( 30, 16 ) ;
    printf ( "4. Delete Records" ) ;
    gotoxy ( 30, 18 ) ;
    printf ( "0. Exit" ) ;
    gotoxy ( 30, 20 ) ;
    printf ( "Your choice" ) ;
    fflush ( stdin ) ;
    choice = getche( ) ;
    switch ( choice )
    {
    case '1' :
    fseek ( fp, 0 , SEEK_END ) ;
    another = 'Y' ;
    while ( another == 'Y' )
    {
    printf ( "\nEnter name, age and basic sal. " ) ;
    scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
    fwrite ( &e, recsize, 1, fp ) ;
    printf ( "\nAdd another Record (Y/N) " ) ;
    fflush ( stdin ) ;
    another = getche( ) ;
    }
    break ;
    case '2' :
    rewind ( fp ) ;
    while ( fread ( &e, recsize, 1, fp ) == 1 )
    printf ( "\n%s %d %f", e.name, e.age, e.bs ) ;
    break ;
    case '3' :
    another = 'Y' ;
    while ( another == 'Y' )
    {
    printf ( "\nEnter name of employee to modify " ) ;
    scanf ( "%s", empname ) ;
    rewind ( fp ) ;
    while ( fread ( &e, recsize, 1, fp ) == 1 )
    {
    if ( strcmp ( e.name, empname ) == 0 )
    {
    printf ( "\nEnter new name, age & bs" ) ;
    scanf ( "%s %d %f", e.name, &e.age,
    &e.bs ) ;
    fseek ( fp, - recsize, SEEK_CUR ) ;
    fwrite ( &e, recsize, 1, fp ) ;
    break ;
    }
    }
    printf ( "\nModify another Record (Y/N) " ) ;
    fflush ( stdin ) ;
    another = getche( ) ;
    }
    break ;
    case '4' :
    another = 'Y' ;
    while ( another == 'Y' )
    {
    printf ( "\nEnter name of employee to delete " ) ;
    scanf ( "%s", empname ) ;
    ft = fopen ( "TEMP.DAT", "wb" ) ;
    rewind ( fp ) ;
    while ( fread ( &e, recsize, 1, fp ) == 1 )
    {
    if ( strcmp ( e.name, empname ) != 0 )
    fwrite ( &e, recsize, 1, ft ) ;
    }
    fclose ( fp ) ;
    fclose ( ft ) ;
    remove ( "EMP.DAT" ) ;
    rename ( "TEMP.DAT", "EMP.DAT" ) ;
    fp = fopen ( "EMP.DAT", "rb+" ) ;
    printf ( "Delete another Record (Y/N) " ) ;
    fflush ( stdin ) ;
    another = getche( ) ;
    }
    break ;
    case '0' :
    fclose ( fp ) ;
    }
    }
    }
    
    //the following I added on ur suggestion to make gotoxy work...
    void gotoxy(int x,int y)
    {
        printf("%c%d;%df",0x1B,y,x);
    }
    the program compiles and works, but I don't think it works the way its supposed to...

    here's a screenshot of what it shows after compiling (I don't think it should be like that)

    Attached Images
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    u have not added ] after %c that is problem.
    add ] after %c then it will work.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    Originally Posted by eramit2010
    u have not added ] after %c that is problem.
    add ] after %c then it will work.
    doesn't seem to work either way.. :confused:
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    it looks like this method is not working in codeblock but it working in gcc compiler in linux.
    i checked ur code in my gcc compiler.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    some one make it work on windows codeblocks 64-bit !
  20. #11
  21. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    gotoxy() is part of the old conio.h library that was distributed in TurboC and many other compilers. However, it requires that you #include conio.h, assuming that your compiler has it.

    What eramit2010 showed you uses the old ANSI escape codes, which old-time DOS-ers (such as myself) remember from the system driver that we'd always have to boot up with, ANSI.SYS . The 0x1B, which is 27 decimal, is the Escape control character (ESC -- your keyboard even has a key for it) in ASCII. When a properly programmed device encounters an ESC in its data stream, it interprets the characters that follow as part of a command rather than data to be displayed, hence the very arcane format of the command. I think I once played with it briefly about 30 years ago and I do remember that some printers, such as the Epson, accepted escape codes as formatting commands. I also recall that in order to use escape codes to control your console display, you had to have the driver installed and loaded, hence the inclusion of ANSI.SYS in the MS-DOS boot-up sequence. I have no idea whether Windows currently supports escape codes.

    SetConsoleCursorPosition is part of Microsoft's "character-mode console API", coverage of which starts at http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx. This is the preferred method in Windows and it is supported on all Windows machines, which makes it portable in Windows. You can Google for tutorials or even for examples of using SetConsoleCursorPosition: about 20,000 hits at http://www.google.com/#hl=en&sclient...w=1404&bih=770.

    What compiler does codeblocks use? I thought it was the MinGW gcc Windows port, though I would guess a later version than Dev-C++ used. My version of MinGW gcc (gcc version 2.95.3-6 (mingw special)) included in the INCLUDE directory two files: conio.h and conio.c. From them, I created the object file, conio.o, and can add conio functions to my programs that way. That conio.c file uses Microsoft's character-mode console API, but it's not a complete implementation. You might want to check your compiler's directories to see whether you do have conio support there.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    Originally Posted by dwise1_aol
    gotoxy() is part of the old conio.h library that was distributed in TurboC and many other compilers. However, it requires that you #include conio.h, assuming that your compiler has it.

    What eramit2010 showed you uses the old ANSI escape codes, which old-time DOS-ers (such as myself) remember from the system driver that we'd always have to boot up with, ANSI.SYS . The 0x1B, which is 27 decimal, is the Escape control character (ESC -- your keyboard even has a key for it) in ASCII. When a properly programmed device encounters an ESC in its data stream, it interprets the characters that follow as part of a command rather than data to be displayed, hence the very arcane format of the command. I think I once played with it briefly about 30 years ago and I do remember that some printers, such as the Epson, accepted escape codes as formatting commands. I also recall that in order to use escape codes to control your console display, you had to have the driver installed and loaded, hence the inclusion of ANSI.SYS in the MS-DOS boot-up sequence. I have no idea whether Windows currently supports escape codes.

    SetConsoleCursorPosition is part of Microsoft's "character-mode console API", coverage of which starts at http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx. This is the preferred method in Windows and it is supported on all Windows machines, which makes it portable in Windows. You can Google for tutorials or even for examples of using SetConsoleCursorPosition: about 20,000 hits at http://www.google.com/#hl=en&sclient...w=1404&bih=770.

    What compiler does codeblocks use? I thought it was the MinGW gcc Windows port, though I would guess a later version than Dev-C++ used. My version of MinGW gcc (gcc version 2.95.3-6 (mingw special)) included in the INCLUDE directory two files: conio.h and conio.c. From them, I created the object file, conio.o, and can add conio functions to my programs that way. That conio.c file uses Microsoft's character-mode console API, but it's not a complete implementation. You might want to check your compiler's directories to see whether you do have conio support there.
    I have conio.h.

    I am unable to understand setconsolecursorpointer(). I just need a copy/paste solution here. I'm not really that much interested in learning console applications etc..

    And I've checked out a dozen different websites for its examples, I cant understand any of them, I'm new to C. cant I just use it like SetConsoleCursorPointer(x,y) ?
  24. #13
  25. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,376
    Rep Power
    1871
    > I'm not really that much interested in learning console applications etc..
    So why do you care about conio.h and gotoxy?
    You're investing time in learning something obsolete, so why aren't you pushing to learn something more current.



    Use this if you want a win32 console function dressed up as gotoxy
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    Also on the reading list
    http://sourceforge.net/apps/mediawik...le=Indentation
    http://sourceforge.net/apps/mediawik...p?title=Fflush

    Comments on this post

    • zedeneye1 agrees : thank u
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    73
    Rep Power
    2
    Originally Posted by salem
    > I'm not really that much interested in learning console applications etc..
    So why do you care about conio.h and gotoxy?
    You're investing time in learning something obsolete, so why aren't you pushing to learn something more current.



    Use this if you want a win32 console function dressed up as gotoxy
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    Also on the reading list
    http://sourceforge.net/apps/mediawik...le=Indentation
    http://sourceforge.net/apps/mediawik...p?title=Fflush
    EDIT : I've got it working. thankx for the links.
    Last edited by zedeneye1; March 18th, 2013 at 09:10 AM.
  28. #15
  29. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by zedeneye1
    I have conio.h.

    I am unable to understand setconsolecursorpointer(). I just need a copy/paste solution here. I'm not really that much interested in learning console applications etc..
    I know that this is after-the-fact now, but if your compiler (which I still assume is MinGW gcc from my understanding of what codeblocks uses) supports conio, then why not just use that since you're only wanting to be able to run some old programs? There should be no need at this time to worry about doing it more properly with the console API (eg, with SetConsoleCursorPointer()).

    If you're having problems linking in conio, then you should have just said so. conio.h only tells the compiler that there are these functions out there somewhere, whereas a library or source file still needs to be present to provide those functions. My version of MinGW gcc included in the INCLUDE directory a source file for conio, conio.c. If your version does the same, then you would simply add conio.c to your project.

    Originally Posted by zedeneye1
    And I've checked out a dozen different websites for its examples, I cant understand any of them, I'm new to C. cant I just use it like SetConsoleCursorPointer(x,y) ?
    Every function has a prototype that describes what arguments it takes. You can't change that. SetConsoleCursorPointer() doesn't take x and y coordinates, but rather a coordinate structure and a handle to the output file that the console is associated with. Now, you could write a wrapper function that would take x and y and then use them to make the call to SetConsoleCursorPointer(). That is how the author did it in the conio.c source file that came with my version of MinGW gcc:
    Code:
    void
    gotoxy(int x, int y)
    {
      COORD c;
    
      c.X = x - 1;
      c.Y = y - 1;
      SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
    }
    Or for that matter you could have just copied the gotoxy function into your own code. Out of curiosity, just what approach did you end up taking?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo