C stdin question
Hi all
Im learning C, and I have come across an interesting problem. I am using scanf to read a string from stdin, and was wondering how to make the characters being typed stay hidden from the screen? Like when you change your password in linux, what you type is not echoed to the screen but is still registered.
Anyone have any ideas how to do this?
Thanks in advance
getch was close to what I was looking for, getpass was much more suited though, so I used it an it works great. The only problem I have now is that I cant echo to the screen before I call getpass. I have a printf statement, followed by a few variable declarations, followed by the getpass call, and the prinf does not show up until after the last getpass call.
Any ideas?
Vty wrote: More than likely, you need to flush your output buffer.
fflush( stream ); /* Where stream is stdout, stdin, or any other file stream */
Do not use fflush for input streams!!!!
according to the standard C:
int fflush(FILE* stream);
Flushes stream stream and returns zero on success or EOF on error.
Effect undefined for input stream. fflush(NULL) flushes all output streams.
http://www.infosys.utas.edu.au/info/documentation/C/CStdLib.html#fflush
If you wish to clean the input buffer you should try:
scanf("%c%*c", &myVar);
/* This will scan a char, and discart the next one (in most cases its one '\n').
the %*c flag means: read and discart
*/
Or you could do a loop with getc.
spyware wrote: [quote]454447415244 wrote: OFF TOPIC: But why learning C and not directly learning C++ ?!
You obviously never had to work with C. You'll need it later on for many things.[/quote]
No, actually I worked with c for many years… But now I don't use it that much except for some Linux stuff… Now I work with c++ as an object oriented language which is useful for large scale applications… As a good programmer, switching from c++ to c will be an easy job with the little difference in syntax and environement…
[Double post Fix'd]