Solved

reading from stdin

Posted on 2000-03-24
14
235 Views
Last Modified: 2011-09-20
I wanted to retrieve a single character from stdin. (ie. the user strikes only one key and the function returns immediatly)
But I can't remember of any function that does it.
Is there any way to prevent the user from typing as much characters as he wants.
(I'm writing a console application in VC++. I have to use C only.)
0
Comment
Question by:Yo081699
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 32

Expert Comment

by:jhance
Comment Utility
#include <conio.h>
....
char c;
....
if(__kbhit()){
 c = _getch();
}
0
 

Author Comment

by:Yo081699
Comment Utility
I don't like this solution because it relies on the console. Is it possible to achieve this using only the standard library? (which would make my code portable to any environment.)
0
 

Expert Comment

by:balakrishnan_t
Comment Utility
#include "conio.h"
main()
{
 getch();
}

I hope this will satisfy ur need. wait for an keystroke...and return as soon as any key is pressed.
0
 
LVL 3

Expert Comment

by:marcjb
Comment Utility
There is no way to do this using the C Standard.  getch() is not part of the Standard, but is offered on many systems and therefore may offer the portability you seek.

If you want to stick to the Standard, ask for user input, and require it to be ended with a CRLF (hitting enter/return).  Just make sure you have an array big enough to hold extra characters.

Hope this helps,

marc
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
balakrishnan_t,

Thank you for copying my answer and posting it as your own!  

This is NOT how things are done here on Experts Exchange.
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Yo,

You asked:

"retrieve a single character from stdin"

STDIN implies console!

You also said:

"console application in VC++"

VC++ IMPLIES Microsoft WINDOWS!!!

If you want to do this from a Windows application which doesn't have a console, let "expert" balakrishnan_t answer your question.
0
 

Author Comment

by:Yo081699
Comment Utility
balakrishnan_t, jhance is right! You didn't say anything more than him.
As it seems that it's the only solution available and that "many implementations provide getch" I'll take jhance answer.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Yo081699
Comment Utility
JHANCE, please copy-paste again your answer. I'm not allowed to accept a previously rejected answer.
0
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
Comment Utility
#include <conio.h>
.....
char c;
.....
if(_kbhit()){
 c = _getch();
}
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Yo,

Thanks for noticing that clown's tactics.

balakrishnan_t,

If you want to become a "real" expert, please come up with your own answers.  Copying someone elses answer to a given question, whether posted as an answer or a comment is very bad form indeed.
0
 

Expert Comment

by:jpjpjp
Comment Utility
Actually, what bala said is a bit different, ie didnt put the if (kbhit())...

If getch() returns right away, no need to check if the kb was hit...

note that, u dont include a system file with "", but with <>...

So, you could use 2 ways, ie:

if (kbhit())
{
   c = getchar();
}

or

c = getch(); /* that is, if it returns right away.. */

I briefly looked at the man page of getch on unix, and i dont really like that function (not MT safe, doesnt like ESC key, etc..), so i would use the if(kbhit()) with the getchar()..

note that kbhit() doesnt exist on unix, but u can use it on windoze or dos..
0
 

Author Comment

by:Yo081699
Comment Utility
Ok, thanks everybody. getch() will do.
0
 

Expert Comment

by:balakrishnan_t
Comment Utility
jhance,

      I didn't copy ur answer... As  jpjpjp said, i thought my answer is a different from urs. If u don't know the different , please keep quiet, don't put unnecessary blaming on theres. Actually i feel that my answer works on under all platform while urs causes some problem under unix as jpjpjp said...So please mind ur words...

     
0
 

Expert Comment

by:balakrishnan_t
Comment Utility
jhance,

      I didn't copy ur answer... As  jpjpjp said, i thought my answer is a different from urs. If u don't know the different , please keep quiet, don't put unnecessary blaming on theres. Actually i feel that my answer works on under all platform while urs causes some problem under unix as jpjpjp said...So please mind ur words...

     
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now