LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-19-2004, 09:15 PM   #1
Eileen
Member
 
Registered: Oct 2004
Posts: 99

Rep: Reputation: 15
Please teach me how to use getc(stdin)


Hello everyone,
I am a newbie to linux and have been trying to write a program using
getc(stdin). Its a program on password whereby I didnt want to display the
user input password. But my following program is not working properly.
Please help me!!

Source code:
#include <stdio.h>
#include <ncurses.h>

int main()
{
int c;

printf("PASSWORD: ");
initscr();
noecho();
while((c=getc(stdin)) != '\n')
{
printf("%d",c);
}
refresh();
endwin();
exit(0);
}

I compile this way:
# gcc -o temp temp.c -lncurses
# ./temp
# PASSWORD: <- I type in some characters thru keyboard and
then I press Enter.

ERROR: Nothing happens. It just stop here. I have to do a keyboard
CTRL-C to see the prompt again.

Could someone please teach me how to properly use getch(stdin)??
Thank you so much!!
 
Old 12-19-2004, 09:21 PM   #2
niknah
Member
 
Registered: Dec 2002
Location: In front of a computer
Distribution: UPS, DHL, FedEx
Posts: 466

Rep: Reputation: 38
'\n' is the line feed character.
when some one press return they send '\r' (carriage return)
 
Old 12-19-2004, 09:27 PM   #3
Eileen
Member
 
Registered: Oct 2004
Posts: 99

Original Poster
Rep: Reputation: 15
Thank you very much!!
 
Old 12-19-2004, 10:20 PM   #4
Eileen
Member
 
Registered: Oct 2004
Posts: 99

Original Poster
Rep: Reputation: 15
Hi again,

I couldn't printf whatever message in my while loop. Could someone please help me again??

Here is my source code:
#include<stdin.h>
#include<ncurses.h>
int main()
{
int c;

initscr();
noecho();
while((c=getc(stdin)) != '\r')
{
printf("Hello!");
}
refresh();
endwin();
return 0;
}

My Hello cannot be printed out after ./temp
If I put the printf("Hello") after the refresh() and endwin(), then I am able to print out this Hello. Could someone please correct my source code? Also what is the meaning of refresh() and the endwin().

Thank you very much for any kind help!!
 
Old 12-19-2004, 10:51 PM   #5
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
The refresh() and endwin() are necessary functions for the ncurses stuff. Why are you using ncurses anyway? I'd think it would be easier without it.
 
Old 12-19-2004, 10:57 PM   #6
Eileen
Member
 
Registered: Oct 2004
Posts: 99

Original Poster
Rep: Reputation: 15
Really? I am not sure because I never use getc(stdin) in the linux environment before.
I thought this is a "MUST" include <ncurses.h> whenever getc(stdin) is used in the program. What actually is wrong with my program, the printf??
Appreciate any help given!!
 
Old 12-19-2004, 11:01 PM   #7
niknah
Member
 
Registered: Dec 2002
Location: In front of a computer
Distribution: UPS, DHL, FedEx
Posts: 466

Rep: Reputation: 38
no, you don't need ncurses to use getc().


if you don't want to use ncurses you can use tcgetattr/tcsetattr to turn off the echo, but you won't be able to draw pretty gui-ish things without ncurses.

for the ncurses, add refresh() after printf to see what you've printed.
 
Old 12-19-2004, 11:08 PM   #8
Eileen
Member
 
Registered: Oct 2004
Posts: 99

Original Poster
Rep: Reputation: 15
Sorry for all my silly questions asked here.
Could you please kindly teach me on how to improve my program in such a way that the user keyboard input is not shown? This is why I am using getc(stdin).

I do not know how to use tcgetattr. :-D
 
Old 12-19-2004, 11:12 PM   #9
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Your program was not printing anything for two reasons. First, you used the noecho() to prevent the password from echoing to the screen. Second, you need to use printw when writing to the curses buffer. The refresh writes the buffer to the screen.

Here is a version that actually prints something:
Code:
#include <ncurses.h>

int main()
{
  int c;

  initscr();
  noecho();

  while((c=getc(stdin)) != '\r')
  {
  echo();
  printw("Hello!\n");

  }


  refresh();
  endwin();

  return 0;
}
 
Old 12-19-2004, 11:26 PM   #10
Eileen
Member
 
Registered: Oct 2004
Posts: 99

Original Poster
Rep: Reputation: 15
Hi, Thank You very very much for your help! But I still not able to print out the Hello! :"( Could you pleasssseeee help again?
 
Old 12-19-2004, 11:28 PM   #11
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Hmmm.. I see it. What's your terminal type? set | grep TERM

I'd likely do it this way, without curses.
Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

struct termios save;

void echoOff()
{
  struct termios term;
  tcgetattr( 0, &save );
  term = save;
  term.c_lflag &= (~ECHO);
  tcsetattr( 0, TCSANOW, &term );
}

void echoOn()
{
  tcsetattr( 0, TCSANOW, &save );
}

main()
{

  char password[30];

  echoOff();
  fgets( password, 30, stdin );
  echoOn();

  printf("Hello dude, your Password was: %s\n", password );

}
 
Old 12-19-2004, 11:38 PM   #12
Eileen
Member
 
Registered: Oct 2004
Posts: 99

Original Poster
Rep: Reputation: 15
#set | grep TERM

COLORTERM=gnome-terminal
TERM=xterm

Does this means that my PC cannot use ncurses.h? I am going to try out your program and post the result again shortly.
Thank you so much!!
 
Old 12-20-2004, 12:34 AM   #13
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Peeling through my Stevens book, I found a getpass function. Lucky for you it's available here:
http://www.yendor.com/programming/un...mios/getpass.c

It's an excellent book if you are serious about UNIX programming in C.

Advanced Programming in the UNIX Environment. By Richard Stevens.

Another good one:

Advanced UNIX programming Second Edition. By Marc J. Rochkind
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
teach me how to get online demmylls Linux - Networking 4 11-22-2004 04:31 AM
where is some material to teach linux? eantoranz Linux - General 3 11-02-2004 12:20 PM
teach me the better way slack66 Linux - Newbie 3 02-15-2004 10:55 PM
Teach me Alucard9 Linux - Newbie 5 12-07-2003 09:16 AM
teach me to fish bandofmercy Linux - General 6 04-10-2003 01:40 PM


All times are GMT -5. The time now is 04:42 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration