Solved

reading from stdin

Posted on 2000-03-24
14
238 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
ID: 2652712
#include <conio.h>
....
char c;
....
if(__kbhit()){
 c = _getch();
}
0
 

Author Comment

by:Yo081699
ID: 2653113
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
ID: 2653138
#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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 3

Expert Comment

by:marcjb
ID: 2653566
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
ID: 2653804
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
ID: 2653820
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
ID: 2654132
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
 

Author Comment

by:Yo081699
ID: 2654152
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
ID: 2654236
#include <conio.h>
.....
char c;
.....
if(_kbhit()){
 c = _getch();
}
0
 
LVL 32

Expert Comment

by:jhance
ID: 2654242
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
ID: 2654976
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
ID: 2655374
Ok, thanks everybody. getch() will do.
0
 

Expert Comment

by:balakrishnan_t
ID: 2656831
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
ID: 2656832
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

786 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