This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: About Keyboards - URGENT!!!!


What about using getc or getchar in a loop which iterates n times?

I'm not going to give more of the algorithm out, but recall doing something
like this for a C course in school and both of these functions are ANSI, which
means that they should be available on all platforms where the C compiler is
ANSI, and (I believe) the same result would occur on all such platforms;
although, supporting all platforms might not be important, especially for a
university or college course.

If getc and or getchar work, then this would eliminate the need for additional
overhead like fcntl, but you'ld need to look up the definition of both of
these, because I think that one may need the user to press Enter or Return,
whilst the function other doesn't; something like getc being irrelevant of
Return or Enter while getchar needs this key to be pressed (you'ld need to
verify).

It's been a long time since I've used these, but these were common for simpler
ways of getting single char user input from stdin.  If the stdin happens to be
from some other location than the default, then I believe that there are
counterpart f functions for either or both of these, for example fgetc.

There's a purpose for read, but it's for lowest level work or tasks; perhaps
also for more assured portability.

getc or getchar may be adequate in this case, and I believe that these use
read while providing a simpler user or programmer interface when special cases
aren't relevant.  For example, an OS probably uses read, while many or most
user programs adequately use getc and or getchar, i.e., it depends on the
purpose; although, ANSI only says what a function must do, instead of how it
must be done, which means that ANSI'd functions can vary wrt things like side
effects.  If that's important, then use read.

Look up the getc, getchar, read, and fcntl functions in this order, to go from
simplest to least (getc and getchar are very similar).  If others are
relevant, then the descriptions you'll read should indicate this.

If you're programming C on Unix or Linux and have Richard Stevens' book on
Programming C for Unix available, then look these functions up in this book.
This book may be adequate for programming in MS OSs, but you'ld need to learn
from trial.

If the question is for a university or college course, then such questions
should be figured out by studying the book.  These functions would be quick to
find by simply browsing through the index, at least if the book is decent.
The way to approach this kind of search is to ask yourself what you want to do
and in this case it's related to character, or string (char is subset of
string, but you can surely skip looking up string in the index, if you have a
decent book), and get or read.  Using this kind of approach, answers can be
quickly found by simply browsing the index (of a decent book) and this can
often be quicker than using mailing lists.

I agree with Virgil that this mailing list is probably not intended for such
questions.

mike corbeil


Virgil Palanciuc wrote:

>    As a first note, I don't think this is the right place for this kind of
> questions.
>
> > I need a function that reads n leters from the keyboard
> > that folow some rule.
> > [....]
>
>   I don't think anybody will write the program for you, so you should have
> given a much shorter description for your problem.
>   You can use read(...)  to do the job, and specify '0' as the first
> parameter (0 means stdin - i.e. the keybord, generally). You should also do
> something like
>    fcntl(0,F_SETFL,O_RDONLY|O_NONBLOCK);
> before any call to your function. (I'm not sure the fcntl call is correct,
> you should check the manpages).
> > And, is desired too that the function runs on Windows/Dos/Unix/Linux
> > systems....
>     AFAICT, this will work both on windows/dos and on linux. However,
> depending on the compiler you use (under dos), you may need to include
> different header files.
>
> > Need response urgent.....
> > Thanks...
> > --
> > Nelson Guedes Paulo Junior
> > E-mail:  <npaulo@linux.ime.usp.br>   UIN: 2489382 (Tender AML)
> > HomePage: (Em eterna construção)
> > http://members.xoom.com/DuneKiller/index2.html
>
>    Virgil.




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]