Thread: Can someone tell me if I've lost the plot

  1. #1
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236

    Can someone tell me if I've lost the plot

    This is a discussion that's been going on on another board that I visit every now and again.

    Code:
    Poster1
    
       .....
    
       Now if someone can tell me how to take a single character 
       without requiring enter to be pressed, that'd also be ace.
    
       Cheers.
    
    Poster2
    
       Well, the only function I know is getch()... not sure if it's the 
       best way to do it though, haven't done C code for aaaages
    
    Azuth
     
       getc, fgetc, getchar - getch() would likely work, but is not
       standard.
    
    Poster3
     
       only on windows.. you need to do termios stuff on unix 
       like:
    
      #include <termios.h>
    
      struct termios term;
    
      tcgetattr(0, &term)
      term.c_lflag &= ~ICANON; // turn off cooked mode
      term.c_cc[VMIN] = 0; // no minimum chars
      term.c_cc[VTIME] = 0; // no hold-keys for time
      tcsetattr(0, TCSANOW, &term)
    
      // read one char/key from tty
      i = read(0, &ch, 1);
    
      tcsetattr(0, TCSANOW, &origterm) // restore original settings
    
    Azuth
     
       *Cough* *Hack* *Splutter*
    
       You're saying that I can't read a single character under unix 
       using only ANSI C? Or did I miss something?
    Now I'll admit the only code I've ever worked with on a *nix platform has been to do with quake, and I don't have a *nix box set up at home right now to try, so I may be mistaken, but surely I could write the code to read a single char in DOS in ANSI C, and compile that on a unix box & run it without much issue???
    Demonographic rhinology is not the only possible outcome, but why take the chance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Can someone tell me if I've lost the plot

    Originally posted by Azuth

    Now I'll admit the only code I've ever worked with on a *nix platform has been to do with quake, and I don't have a *nix box set up at home right now to try, so I may be mistaken, but surely I could write the code to read a single char in DOS in ANSI C, and compile that on a unix box & run it without much issue???
    Nope. Well, yes. But no.

    You could, but it'd be ugly:

    Code:
    #ifdef NIX
    #include nix_specific_headers
    int mygetc( )
    {
        do_stuff( here );
    }
    #else
    #include dos_specific_headers
    int mygetc( )
    {
        do_stuff( here );
    }
    #endif
    
    use = mygetc( );
    So yeah, you can do it. But not as simply as you want to. Your best bet in *nix is to use the curses library.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing and saving a plot
    By thetinman in forum C# Programming
    Replies: 2
    Last Post: 01-07-2009, 08:27 AM
  2. plot array outside of C-program
    By ojschubert in forum C Programming
    Replies: 2
    Last Post: 02-28-2005, 12:28 AM
  3. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  4. lost disk
    By Benzakhar in forum Linux Programming
    Replies: 7
    Last Post: 01-11-2004, 06:18 PM
  5. API, LOST... help
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 03-13-2002, 03:19 PM