Ada.Text_IO.Look_Ahead
Motivation for Look_Ahead
- Allows viewing an input character but not consuming it
- The character can be viewed again later by the same routine or a different routine
- Example: compiling 123+456 allows + to signify that the end of the number is reached, and the
the plus to be recognized later
- Allows the proper input routine to be selected based on the type of character seen by look_ahead
look_ahead(c, EOL);
if EOL then
skip_line;
else -- not EOL
if is_digit(c) then
-- do a numeric get
else
-- do a character get
end if;
end if;
Procedure Look_Ahead
- Declaration:
look_Ahead(C: out Character; EOL: out Boolean)
- Parameters
-
c: if EOL is false, c returns,
but does not consume, the next character
in the input stream
-
c: if EOL is true, c is NOT set
-
EOL: returns true if current input stream is at the end
of line, false otherwise
- Warning: For Look_Ahead to work properly your data file should be
created on the same OS as your program is compiled under
Procedure Look_Ahead Behavior
- If look_Ahead is called when the input marker is
at the end of the line, EOL will return true and C will not be valid
- If look_Ahead is called when the input marker is NOT at the end of the
line, EOL will return false and C will contain the next
value of input (ie the character that will be returned by the next get of a
character), without moving the input marker
- Repeated calls to look_Ahead without intervening calls to get (or
other input routines) return the same values
Procedure Look_Ahead Examples
Other Languages: Java
- java.io.PushbackInputStream(InputStream) - constructs a stream that can be used to push back one byte
- java.io.PushbackInputStream(InputStream, int size) - constructs a stream
that can be used to push back size bytes
- void unread(int b) pushes back the byte b which will be input again by the
next read. Only one byte can be pushed back at a time.
Other Languages: C++
- C++ has unget and putback
Other Languages: C