Class In -- Formatted Input
This class allows you to read formatted data either from the console
or from a stream. To use it, simply compile it with your own classes:
csc In.cs MyClass.cs
Initially, the input stream is the console. In.Open(s) makes s the new
input stream. In.Close() switches back to the previous input stream. Calls to
In.Open() and In.Close may be nested.
When a method reads from the console, it blocks until the user has entered
a sequence of characters terminated by the return key. All methods read
from this input buffer (which includes also the terminating '\r' and '\n') until the
buffer is fully consumed. When a method tries to read beyond the end
of the buffer, it blocks again waiting for the next buffer.
End of file detection: When reading from the console, eof can be
signaled by typing ctrl-Z at the beginning of a new line. When reading from a file,
eof occurs when an attempt is made to read beyond the end of the file.
In either case In.Done returns false if the requested data could not
be read because of eof.
- public const char eof = '\uffff';
- Returned by In.Read() when the user reads beyond the end of the file.
- public static bool Done { get; }
- Signals the success of the previous read operation.
- public static void Open(string path);
- Opens the file with the given path as the current input stream.
Any exceptions must be caught by the caller.
- public static void Open(Stream s);
- Opens s as the current input stream.
Any exceptions must be caught by the caller.
- public static void Close();
- Closes the current input stream and switches back to console input.
- public static char Read();
- Reads the next character from the input stream. When no character could
be read this method returns eof and In.Done is false.
- public static int ReadInt();
- This method skips white space and tries to read an integer. If the
text does not contain an integer or if the number is too big, the
value 0 is returned and In.Done yields false.
An integer is a sequence of digits, possibly preceded by '-'.
- public static long ReadLong();
- This method skips white space and tries to read a long integer. If the
text does not contain a number or if the number is too big, the
value 0 is returned and In.Done yields false.
A long integer is a sequence of digits, possibly preceded by '-'.
- public static float ReadFloat();
- This method skips white space and tries to read a float value. If the
text does not contain a float value or if the number is not well-formed,
the value 0f is returned and In.Done yields false.
An float value is as specified in the C# language description. It may
be preceded by a '+' or a '-'.
- public static double ReadDouble();
- This method skips white space and tries to read a double value. If the
text does not contain a double value or if the number is not well-formed,
the value 0.0 is returned and In.Done yields false.
An double value is as specified in the C# language description. It may
be preceded by a '+' or a '-'.
- public static bool ReadBool();
- This method skips white space and tries to read an identifier. If its value
is "true" the method returns true otherwise false.
If the identifier is neither
"true" nor "false" In.Done yields false.
- public static string ReadIdent();
- This method skips white space and tries to read an identifier starting
with a letter or '_' and continuing with letters, digits or '_'. If a token of this
structure could be read, it is returned otherwise the empty string is
returned and In.Done yields false.
- public static string ReadString();
- This method skips white space and tries to read a string in the form "...".
It can be used to read pieces of text that contain white space.
- public static string ReadWord();
- This method skips white space and tries to read a word consisting of
all characters up to the next white space or to the end of the file.
If a token of this structure could be read, it is returned otherwise
an empty string is returned and In.Done yields false.
- public static string ReadLine();
- This method reads the rest of the current line (including '\r' and '\n') and
returns it (excluding '\r' and '\n'). A line may be empty.
- public static string ReadFile();
- This method reads from the current position to the end of the
input stream and returns its text in a single large string. In.Done yields
always true.
- public static char Peek();
- This method skips white space and returns the next character without removing
it from the input stream. It can be used to find out, what token comes next
in the input stream.
Example. The following code reads a sequence of numbers from a file
named input.txt:
In.Open("input.txt");
int n = In.ReadInt();
while (In.Done) {
Console.WriteLine("n = " + n);
n = In.ReadInt();
}