comp.lang.ada
 help / color / mirror / Atom feed
* put a character back to the input stream
@ 1999-10-23  0:00 Frank Shixiang Yan
  1999-10-23  0:00 ` nabbasi
  1999-10-25  0:00 ` Ted Dennison
  0 siblings, 2 replies; 4+ messages in thread
From: Frank Shixiang Yan @ 1999-10-23  0:00 UTC (permalink / raw)


is there any function in ada which can put a read character back to the
input stream like cin.putback(c) in c++. it happens when i have a input
which i read a character each time
"name + 342"
when i found a digit, i know the whole thing's gonna be a number, so i
put the read digit back, then read the whole number. the same thing
happens for the string too. so i need a function to put back the current
read character back to avoid lose the first character.

thanks




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: put a character back to the input stream
  1999-10-23  0:00 put a character back to the input stream Frank Shixiang Yan
@ 1999-10-23  0:00 ` nabbasi
  1999-10-24  0:00   ` Robert Dewar
  1999-10-25  0:00 ` Ted Dennison
  1 sibling, 1 reply; 4+ messages in thread
From: nabbasi @ 1999-10-23  0:00 UTC (permalink / raw)


In article <3811B324.D0CC49A3@hotmail.com>, Frank says...
>
>is there any function in ada which can put a read character back to the
>input stream like cin.putback(c) in c++. it happens when i have a input
>which i read a character each time
>"name + 342"
 
package  Interfaces.C_Streams has function :

  function ungetc (c : int; stream : FILEs) return int;

(and all the other standard C stream functions.) 

\nasser





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: put a character back to the input stream
  1999-10-23  0:00 ` nabbasi
@ 1999-10-24  0:00   ` Robert Dewar
  0 siblings, 0 replies; 4+ messages in thread
From: Robert Dewar @ 1999-10-24  0:00 UTC (permalink / raw)


In article <7ut91r$nih@drn.newsguy.com>,
  nabbasi@pacble.net.NOSPAM wrote:
> package  Interfaces.C_Streams has function :
>
>   function ungetc (c : int; stream : FILEs) return int;
>
> (and all the other standard C stream functions.)

Yes, you could use this, but it is hard work (and of course
GNAT-specific). You have to use the special functions provided
in GNAT to extract the stream identifier from a Text_IO file
(these are very useful in such situations, see the GNAT
reference manual for details). But really this is entirely
the wrong direction, and is a consequence of following an
incorrect suggestion of how to solve the original problem.

The proper response to the original question is to use
Text_IO.Look_Ahead, which is the standard facility provided
to solve this problem. In Ada, rather than read the character
and unread it (which is a bit of a kludgy concept, and there
are typically limitations on how many times unread can be
called), the paradigm is to look ahead at the next character
without consuming it. This will solve your problem in a
perfectly neat way, neater than the corresponding C, since
you won't need to remember to do an ungetc.

During the design, the veiwpoint was that look ahead was a
better way to provide the required underlying functionality
for solving problems of exactly this kind than the "unread"
approach.

Anyway, thanks for describing not only the solution you were
seeking (unread) but also the underlying problem (figuring
out what to read next). That allows us to give the appropriate
response.

Robert Dewar


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: put a character back to the input stream
  1999-10-23  0:00 put a character back to the input stream Frank Shixiang Yan
  1999-10-23  0:00 ` nabbasi
@ 1999-10-25  0:00 ` Ted Dennison
  1 sibling, 0 replies; 4+ messages in thread
From: Ted Dennison @ 1999-10-25  0:00 UTC (permalink / raw)


In article <3811B324.D0CC49A3@hotmail.com>,
  sy278@is9.nyu.edu wrote:
> which i read a character each time
> "name + 342"
> when i found a digit, i know the whole thing's gonna be a number, so i
> put the read digit back, then read the whole number. the same thing
> happens for the string too. so i need a function to put back the
> current read character back to avoid lose the first character.

It sounds like you are doing lexical analysis on a stream of characters.
That's exactly what OpenToken was created to do. It will handle
all the minutia about reading, analyzing, and buffering characters for
you. Unless this is a homework assignment, I encourage you to check it
out at http://www.telepath.com/dennison/Ted/OpenToken/OpenToken.html

--
T.E.D.


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-10-25  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-10-23  0:00 put a character back to the input stream Frank Shixiang Yan
1999-10-23  0:00 ` nabbasi
1999-10-24  0:00   ` Robert Dewar
1999-10-25  0:00 ` Ted Dennison

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox