LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Please teach me how to use getc(stdin) (http://www.linuxquestions.org/questions/programming-9/please-teach-me-how-to-use-getc-stdin-268232/)

Eileen 12-19-2004 09:15 PM

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!!

niknah 12-19-2004 09:21 PM

'\n' is the line feed character.
when some one press return they send '\r' (carriage return)

Eileen 12-19-2004 09:27 PM

Thank you very much!!

Eileen 12-19-2004 10:20 PM

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!!

crabboy 12-19-2004 10:51 PM

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.

Eileen 12-19-2004 10:57 PM

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!!

niknah 12-19-2004 11:01 PM

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.

Eileen 12-19-2004 11:08 PM

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

crabboy 12-19-2004 11:12 PM

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;
}


Eileen 12-19-2004 11:26 PM

Hi, Thank You very very much for your help! But I still not able to print out the Hello! :"( Could you pleasssseeee help again?

crabboy 12-19-2004 11:28 PM

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 );

}


Eileen 12-19-2004 11:38 PM

#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!!

crabboy 12-20-2004 12:34 AM

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


All times are GMT -5. The time now is 02:36 AM.