[comp.lang.c] `Extra' newline when using getchar

mouse@mcgill-vision.UUCP (der Mouse) (03/07/88)

In article <285@sp7040.UUCP>, jsp@sp7040.UUCP (John Peters) writes:
> In article <1653@ssc-vax.UUCP>, dmg@ssc-vax.UUCP (David Geary) writes:
>> I've been wondering about this for a long time.
>> [code that uses getchar() to read characters]
>> [David is puzzled about the extra newline at the end of a line]
> This does indead leave the newline in the buffer.

Quite so.

> [recommends a method involving]
>     char	a[5];
> [...]
> 	gets (a);

> This gets ride of certain other evils such as accidentally entering
> more than one character.

Except that if you enter more than four characters, it breaks badly.
(It overwrites whatever happens to lie past a[].)  If you must use
gets-type routines, you should always use fgets(), to which you pass
the length of the buffer, so it can avoid overflowing it.

> Since it sounds like you are wanting just one character at a time how
> about considering putting the system in raw mode so that when you do
> the getchar it comes right back at you.  Why wait an force the user
> to hit a charrage return.

Cbreak mode is probably better than raw mode.  Have you ever written a
program that uses raw mode and then discovered a bug in the handling of
the command that's supposed to get you out?  Great fun, isn't it?

> 	DISCLAIMER:  My spelling is my own and boy does the company hate it!

I can see why.

					der Mouse

			uucp: mouse@mcgill-vision.uucp
			arpa: mouse@larry.mcrcim.mcgill.edu

rcvie@tuvie (ELIN Forsch.z.) (03/24/88)

In article <976@mcgill-vision.UUCP> mouse@mcgill-vision.UUCP (der Mouse) writes:
>> Since it sounds like you are wanting just one character at a time how
>> about considering putting the system in raw mode so that when you do
>> the getchar it comes right back at you.  Why wait an force the user
>> to hit a charrage return.
>
>Cbreak mode is probably better than raw mode.  Have you ever written a
>program that uses raw mode and then discovered a bug in the handling of
>the command that's supposed to get you out?  Great fun, isn't it?
>

Would all you ignorant UNIX-users out there please recognize that there exist
other operating systems as well that cannot be put into `raw mode',
`cbreak mode' or whatever! C is ***not*** expected to work solely under
UNIX! One of the best features (if not the best at all) of C is its
availability on so many machines with so many OS which offers the
possibility to have many many programs working on many many computers
with just copying some source text and recompilation (if you keep
track of the rules for portable programming :-) ). This is the (only)
reason why my company takes the risk of using such a dangerous HLL.
If you today buy a new system (new in the sence you did not have it yet,
but also in the sence it is devellopped newly) the only compiler and run-time
system you can (almost) be sure to be offered for it is C. But very
seldom UNIX. And thus very seldom UNIX terminal drivers. And thus very
seldom ... . Therefore I ask all you UNIX-folks out there, please be
careful with sentences like "put the system in cbreak mode" or 
"if you do not like C or if it is not appropriate just use any other
programming language" etc. Some people really would like to do so but
there is no chance. (And there is a comp.unix newsgroup as well.)


		Dietmar Weickert,
			ALCATEL-ELIN Research Center, Vienna, Austria.

ned@ghostwheel.UUCP (Ned Nowotny) (03/26/88)

In article <596@tuvie> rcvie@tuvie.UUCP (Alcatel-ELIN Forsch.z.) writes:
>In article <976@mcgill-vision.UUCP> mouse@mcgill-vision.UUCP (der Mouse) writes:
>>> Since it sounds like you are wanting just one character at a time how
>>> about considering putting the system in raw mode so that when you do
>>> the getchar it comes right back at you.  Why wait an force the user
>>> to hit a charrage return.
>>
>>Cbreak mode is probably better than raw mode.  Have you ever written a
>>program that uses raw mode and then discovered a bug in the handling of
>>the command that's supposed to get you out?  Great fun, isn't it?
>>
>
>Would all you ignorant UNIX-users out there please recognize that there exist
>other operating systems as well that cannot be put into `raw mode',
>`cbreak mode' or whatever! C is ***not*** expected to work solely under
>UNIX! [More flamage in the same vein deleted.]

Rather than flaming away, try giving an operating system independent method
of getting getchar() to return a character immediately upon typing the
character.  When you fail, you might appreciate why people responded with
operating system specific solutions.  Now, maybe you can come up with an
operating system independent replacement for getchar() with conditional
compilation for all known systems, but I would be surprised if even then you
could succeed in creating a universally portable method of handling character-
at-a-time I/O.

Discussing C issues independent of operating system is desirable, but some
issues (e.g. I/O) are necessarily operating system dependent.  If you must
flame people for being too system oriented, consider the numerous messages
which debate the relative merits of Turbo C vs. Microsoft C.  Certainly this
is more combustible material.
-- 

Ned Nowotny (ned@ghostwheel.aca.mcc.com.UUCP)