Use scanf fuction it works better.. -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] On Behalf Of fred smith Sent: Saturday, April 06, 2002 9:58 AM To: [EMAIL PROTECTED] Subject: [LONG] Re: a little help for newbie in C
On Sat, Apr 06, 2002 at 11:38:28AM -0500, ramzez wrote: > Hi > I'm newbie in C and I want to write a string and later read a char... but i > don't want to see this char (I mean don't echo only for this)... > <.../...> > printf("Hi, i am a string\n") > key=getchar(); //but I don't want to see the character pressed This isn't a C question so much as it is a Unix/Linux question. Why? Because there's no portable way to do do this cross-platform, the C language does not provide the mechanisms you need. However, as long as you're on Linux (or any other reasonably modern Unix platform) you CAN do it. I've added below some excerpts from a rather old copy of the unix.programming.FAQ, one or more of which discusses the sort of thing I think you're driving at: 3. Terminal I/O *************** 3.1 How can I make my program not echo input? ============================================= How can I make my program not echo input, like login does when asking for your password? There is an easy way, and a slightly harder way: The easy way, is to use `getpass()', which is probably found on almost all Unices. It takes a string to use as a prompt. It will read up to an `EOF' or newline and returns a pointer to a static area of memory holding the string typed in. The harder way is to use `tcgetattr()' and `tcsetattr()', both use a `struct termios' to manipulate the terminal. The following two routines should allow echoing, and non-echoing mode. #include <stdlib.h> #include <stdio.h> #include <termios.h> #include <string.h> static struct termios stored; void echo_off(void) { struct termios new; tcgetattr(0,&stored); memcpy(&new, &stored, sizeof(struct termios)); new.c_lflag &= (~ECHO); tcsetattr(0,TCSANOW,&new); return; } void echo_on(void) { tcsetattr(0,TCSANOW,&stored); return; } Both routines used, are defined by the POSIX standard. 3.2 How can I read single characters from the terminal? ======================================================= How can I read single characters from the terminal? My program is always waiting for the user to press `<RETURN>'. Terminals are usually in canonical mode, where input is read in lines after it is edited. You may set this into non-canonical mode, where you set how many characters should be read before input is given to your program. You also may set the timer in non-canonical mode terminals to 0, this timer flushs your buffer at set intervals. By doing this, you can use `getc()' to grab the key pressed immediately by the user. We use `tcgetattr()' and `tcsetattr()' both of which are defined by POSIX to manipulate the `termios' structure. #include <stdlib.h> #include <stdio.h> #include <termios.h> #include <string.h> static struct termios stored; void set_keypress(void) { struct termios new; tcgetattr(0,&stored); memcpy(&new,&stored,sizeof(struct termios)); /* Disable canonical mode, and set buffer size to 1 byte */ new.c_lflag &= (~ICANON); new.c_cc[VTIME] = 0; new.c_cc[VMIN] = 1; tcsetattr(0,TCSANOW,&new); return; } void reset_keypress(void) { tcsetattr(0,TCSANOW,&stored); return; } 3.3 How can I check and see if a key was pressed? ================================================= How can I check and see if a key was pressed? On DOS I use the `kbhit()' function, but there doesn't seem to be an equivalent? If you set the terminal to single-character mode (see previous answer), then (on most systems) you can use `select()' or `poll()' to test for readability. <snip> 3.6.2 Setting up termios flags ------------------------------ Some hints on setting up the termios flags when using a serial device that you've opened yourself (as opposed to using your existing control tty): 3.6.2.1 c_iflag ............... You probably want to set *all* the bits in `c_iflag' to 0, unless you want to use software flow control (ick) in which case you set `IXON' and `IXOFF'. 3.6.2.2 c_oflag ............... Most of the bits of `c_oflag' are hacks of one sort or another to make output to slow terminals work, and as such some newer systems have dropped almost all of them as obsolete (especially all the gory output-padding options). As with `c_iflag', setting everything to 0 is reasonable for most applications. 3.6.2.3 c_cflag ............... When setting the character size, remember to mask using `CSIZE' first; e.g. to set 8-bit characters, use: attr.c_cflag &= ~CSIZE; attr.c_cflag |= CS8; Other important flags found in `c_cflag' that you probably want to turn *on* and `CREAD' and `HUPCL'. If you need to generate even parity, then set `PARENB' and clear `PARODD'; if you need to generate odd parity then set both `PARENB' and `PARODD'. If you don't want parity at all, then make sure `PARENB' is clear. Clear `CSTOPB' unless you actually need to generate two stop bits. Flags for enabling hardware flow control may also be found in `c_cflag', but they aren't standardised (pity). 3.6.2.4 c_lflag ............... Most applications will probably want to turn off `ICANON' (canonical, i.e. line-based, input processing), `ECHO', and `ISIG'. `IEXTEN' is a more complex issue. If you don't turn it off, the implementation is allowed to do nonstandard things (like define additional control characters in `c_cc') that might cause unexpected results, but you might need to leave `IEXTEN' enabled on some systems to get useful features like hardware flow control. 3.6.2.5 c_cc ............ This is an array of characters that have special meanings on input. These characters are given names like `VINTR', `VSTOP' etc.; the names are indexes into the array. (Two of these "characters" are not really characters at all, but control the behaviour of `read()' when `ICANON' is disabled; these are `VMIN' and `VTIME'.) The indexes are often referred to as though they were actual variables, e.g. "set VMIN to 1" actually means "set c_cc[VMIN] to 1". The shorthand is useful and only occasionally confusing. Many of the slots in `c_cc' are only used if some other combination of flags is set: Used only if `ICANON' is set `VEOF', `VEOL', `VERASE', `VKILL' (and also `VEOL2', `VSTATUS' and `VWERASE' if defined and `IEXTEN' is set) Used only if `ISIG' is set `VINTR', `VQUIT', `VSUSP' (and also `VDSUSP' if defined and `IEXTEN' is set) Used only if `IXON' or `IXOFF' is set `VSTOP', `VSTART' Used only if `ICANON' is *not* set `VMIN', `VTIME' Implementations may define additional entries in `c_cc'. It may be prudent to initialise all the entries to `_POSIX_VDISABLE' (the constant `NCCS' gives the array size) before setting the specific values you wish to use. `VMIN' and `VTIME' (which may share slots with `VEOF' and `VEOL' respectively, depending on the implementation) have the following meaning. The value of `VTIME' is (if not 0) always interpreted as a timer in tenths of seconds. `c_cc[VMIN] > 0, c_cc[VTIME] > 0' `read()' will return when either VMIN bytes of input are available, or if at least one character has been read and VTIME has expired between characters, or if interrupted by a signal. `c_cc[VMIN] > 0, c_cc[VTIME] == 0' `read()' will return when VMIN bytes of input are available, or if interrupted. Otherwise it will wait indefinitely. `c_cc[VMIN] == 0, c_cc[VTIME] > 0' `read()' will return as soon as any input is available; if VTIME expires with no data arriving, it will return with no characters read. (This conflicts slightly with the end-of-file indication received in the event of modem hangup; using 1 for VMIN and either `alarm()' or `select()' for a timeout avoids this particular problem.) `c_cc[VMIN] == 0, c_cc[VTIME] == 0' `read()' will always return immediately; if no data is available it will return with no characters read (with the same problem as above). -- ---- Fred Smith -- [EMAIL PROTECTED] ----------------------------- "And he will be called Wonderful Counselor, Mighty God, Everlasting Father, Prince of Peace. Of the increase of his government there will be no end. He will reign on David's throne and over his kingdom, establishing and upholding it with justice and righteousness from that time on and forever." ------------------------------- Isaiah 9:7 (niv) ------------------------------ _______________________________________________ Redhat-list mailing list [EMAIL PROTECTED] https://listman.redhat.com/mailman/listinfo/redhat-list _______________________________________________ Redhat-list mailing list [EMAIL PROTECTED] https://listman.redhat.com/mailman/listinfo/redhat-list