GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Jul-2005, 11:09
amad1337's Avatar
amad1337 amad1337 is offline
Junior Member
 
Join Date: Dec 2004
Posts: 54
amad1337 will become famous soon enough

redirect char input console


I'am working with project that will be a console app.
Idont want to get in detail about my console app but what iam trying to do is to get a password string from the user in the same time i want to put that input char in the password string and output star kind of char in the output.

like this...
password:*******


So guys is there a way to stop the output of char in same time access it from stdin file??

I hope someone understand my question
  #2  
Old 25-Jul-2005, 13:11
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
check this tutorial.

How to hide characters from being displayed on screen as they are keyed in?

I used the linux_getch() function with little modification to print "*" when user is entering the password.

CPP / C++ / C Code:
int ci_getpass(void) 
{
  struct termios oldstuff;
  struct termios newstuff;
  int    inch;
  
  tcgetattr(STDIN_FILENO, &oldstuff);
  newstuff = oldstuff;                  /* save old attributes               */
  newstuff.c_lflag &= ~(ICANON | ECHO); /* reset "canonical" and "echo" flags*/
  tcsetattr(STDIN_FILENO, TCSANOW, &newstuff); /* set new attributes         */
  inch = getchar();
  if (inch=='\n')
  {
	  tcsetattr(STDIN_FILENO, TCSANOW, &oldstuff); /* restore old attributes     */
	  return inch;
  } else {
  printf("*");
  return inch;
  }
}

Also, check this other thread.

Thanks,
  #3  
Old 25-Jul-2005, 13:55
amad1337's Avatar
amad1337 amad1337 is offline
Junior Member
 
Join Date: Dec 2004
Posts: 54
amad1337 will become famous soon enough
thx for the replay....

I frogot to mention that iam on win(xp) os.
<termios.h> is on linux os...

so is't a way to make that hapen.

Iam using visual 6 c++ and my project is win32 console application (my source is only in c).
  #4  
Old 25-Jul-2005, 14:02
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by amad1337
thx for the replay....

I frogot to mention that iam on win(xp) os.
<termios.h> is on linux os...

so is't a way to make that hapen.

Iam using visual 6 c++ and my project is win32 console application (my source is only in c).

The thread in the link was a response to someone who had used getch() in Windows and wanted an equivalent for Linux.

If you #include <conio.h>, you may be able to use getch(). (It is system-dependent. Older Microsoft compilers, including VC++ 6, have it; newer ones may not.)

These are two system-dependent ways of using functions that are not part of the C standard, and, therefore, are not portable, but one or the other may be suitable for your purposes

Regards,

Dave
  #5  
Old 25-Jul-2005, 15:04
amad1337's Avatar
amad1337 amad1337 is offline
Junior Member
 
Join Date: Dec 2004
Posts: 54
amad1337 will become famous soon enough
davekw7x thx very much m8 you saved me from killing my self

I got it fanily
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>
main()
{
    char c;
    
    c = getch();
   
   while(c != '\r')
   {
      if(isalnum(c))
      {
         printf("%c", 42);
      }
      c = getch();
   } 
}
    

damn i love this forum
  #6  
Old 25-Jul-2005, 17:52
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
What if the user doesn't enter an alphanumeric? You should warn them:
CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>
int main()
{
    char c;
    
    c = getch();
   
   while(c != '\r')
   {
      if(isalnum(c))
      {
         putch('*');
      }
      else
      if (c != '\r')
      {
         putch(0x07);
      }
      c = getch();
   } 
   return 0;
}
I also changed your printf()s to remove the huge overhead that printf() demands. putch() is much better for outputting a single character. Output a bell (ctrl-G). There's also \ version which would be more standard. Is it \b? Can't remember.

Also you need int main() and a return in your code.
__________________

Age is unimportant -- except in cheese
  #7  
Old 25-Jul-2005, 18:11
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by WaltP
Output a bell (ctrl-G). There's also \ version which would be more standard. Is it \b? Can't remember.
.

The Standard escape is '\a' (alarm). The effect of putchar('\a') is system-dependent, and may or may not cause a "beep".

Regards,

Dave
  #8  
Old 25-Jul-2005, 22:00
amad1337's Avatar
amad1337 amad1337 is offline
Junior Member
 
Join Date: Dec 2004
Posts: 54
amad1337 will become famous soon enough
thx davekw7x...

the code i wrote was just ex so it wasn't the greates.. The code i will use in my project will handle if the user uses isspace(c).

thx for help
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35
Compiling Errors ToddSAFM C++ Forum 22 18-Dec-2004 11:42
[C] Discarding input Alhazred C Programming Language 8 04-Aug-2004 11:45
(read/write file) newbie need help plz momotx C Programming Language 6 28-Jan-2004 13:40

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.