Follow us on Twitter!
Society leans ever heavily on computers, if you have the power to take out computers you can take out society. - cubeman372
Saturday, April 19, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 33
Guests Online: 29
Members Online: 4

Registered Members: 82839
Newest Member: fezphantom
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C stdin question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 15-02-09 23:24
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


Author

RE: C stdin question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 16-02-09 01:04
You could use ncurses. It has a function for that, I'd have to check out what it does to get that.
Author

RE: C stdin question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 16-02-09 01:14
Use the C call: getch().

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

RE: C stdin question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 16-02-09 05:33
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?


Author

RE: C stdin question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 16-02-09 05:41
More than likely, you need to flush your output buffer.

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

Edited by on 16-02-09 05:41
Author

RE: C stdin question

richohealey
Member



Posts: 1022
Location: #!/usr/local/bin/python
Joined: 01.05.06
Rank:
Monster
Posted on 16-02-09 08:05
Just reading the source to passwd never occurred to you?


bitchohealey at hotmail dot com skype:richohealey www.psych0tik.net
Author

RE: C stdin question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 16-02-09 13:51
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 Smile


Author

RE: C stdin question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 16-02-09 14:17
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:
Code

    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.infosy. . .tml#fflush

If you wish to clean the input buffer you should try:
Code

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.
Author

RE: C stdin question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 16-02-09 23:02
OFF TOPIC:
But why learning C and not directly learning C++ ?!


Author

RE: C stdin question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 17-02-09 03:29
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.


Author

RE: C stdin question

spyware
Member



Posts: 4192
Location: The Netherlands
Joined: 14.04.07
Rank:
God
Warn Level: 90
Posted on 17-02-09 06:17
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.



img507.imageshack.us/img507/3580/spynewsig3il1.png
"The chowner of property." - Zeph
[small]
Widespread intellectual and moral docility may be convenient for leaders in the short term,
but it is suicidal for nations in the long term.
- Carl Sagan
“Since the grid is inescapable, what were the earlier lasers about? Does the corridor have a sense of humor?” - Ebert
[/s
http://bitsofspy.net
Author

RE: C stdin question

richohealey
Member



Posts: 1022
Location: #!/usr/local/bin/python
Joined: 01.05.06
Rank:
Monster
Posted on 17-02-09 12:57
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.




Edited by richohealey on 17-02-09 12:58
bitchohealey at hotmail dot com skype:richohealey www.psych0tik.net
Author

RE: C stdin question


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 17-02-09 16:16
spyware wrote:
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.


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]




Edited by on 17-02-09 16:50