Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C stdin question


ghost's Avatar
0 0

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


ghost's Avatar
0 0

You could use ncurses. It has a function for that, I'd have to check out what it does to get that.


ghost's Avatar
0 0

Use the C call: getch().

This call will return the next value in the input buffer, stdin, without echoing it to the screen.


ghost's Avatar
0 0

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?


ghost's Avatar
0 0

More than likely, you need to flush your output buffer.

fflush( stream ); /* Where stream is stdout, stdin, or any other file stream */


richohealey's Avatar
Python Ninja
0 0

Just reading the source to passwd never occurred to you?


ghost's Avatar
0 0

Yeah, that is an option but I learn by doing, not copying. Flushing stdout worked great, and so does the code now, thanks for all the help :)


ghost's Avatar
0 0

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.


ghost's Avatar
0 0

OFF TOPIC: But why learning C and not directly learning C++ ?!


ghost's Avatar
0 0

I wasnt exactly learning it, just needed to code something up that ran in a minimal unix environment, and C fits that perfectly. Not sure if c++ works with gcc, plus I am looking to learn OpenCL later this year, which is C based.


spyware's Avatar
Banned
0 0

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.


richohealey's Avatar
Python Ninja
0 0

Urgh C++ is overrated.

EDIT: I wasn't suggesting you copy, I was suggesting you learn, the GNU hackers know what they're doing, surprisingly enough.


ghost's Avatar
0 0

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]