2

I need a DLL for users can type password and not echo on the screen. So I use _getch() for getting chars with no echo like this,

        //get character with no echo
        ch = _getch();

and compile the code use microsoft vs2005.
It works on windows server 2003/2008, but on the new windows server 2012, it echos the characters to the screen.
My problem is why _getch() echo characters only on windows server 2012? and how to fix it?

jmuok
  • 340
  • 1
  • 11
  • at last I found `_getch()` works well, the problem is that I use `fgetpos` and `fsetpos` to check if any stream in `stdin`. I replace them with `fseek(stdin,0,SEEK_END)`, its works well in 2012 for now. – jmuok Oct 24 '12 at 02:50
  • Shouldn't you be using `_isatty` to check if the stream is `stdin`? – user541686 Oct 24 '12 at 17:48
  • Thanks for your suggestion.I changed to use `_isatty ` like this `int re = _isatty(_fileno(stdin));` its works. Is `_isatty` better than `fseek`and why?(I only use it on windows os) thanks a lot. – jmuok Oct 25 '12 at 09:01
  • 1
    Yean, `_isatty` is the 'correct' way to do it. I'm not sure what you mean by "why"... I don't think I've ever read anywhere that `fseek` should be used to determine the input type at all. – user541686 Oct 25 '12 at 10:45

1 Answers1

0

Use cin.get() in place of getch() .

Kamal Kafkaesque
  • 57
  • 1
  • 2
  • 7
  • The question is tagged both c and c++; `cin.get()` isn't available in C. – Keith Thompson Nov 16 '12 at 08:43
  • Yes, but (a) if the OP tagged the question as both c and c++, he's probably looking for a solution that will work in both languages, and (b) `cin.get()` doesn't turn off echoing. – Keith Thompson Nov 16 '12 at 09:39