System.Console.Out
) and
the standard error stream (System.Console.Error
).
By default these streams are connected to the console (terminal) from
which the program is run. Anything you write to these streams is by
default sent to the console. However, these streams can be easily
redirected by your shell to other destinations (see below).
You write data to the standard output stream using
System.Console.Out.Write
method (can be also called as
System.Console.Write
),
like this,
double x = 1.23; System.Console.Out.WriteLine($"{x}"); /* or, equivalently */ System.Console.WriteLine($"{x}"); /* or */ System.IO.TextWriter /* or just var */ stdout = System.Console.Out; stdout.WriteLine($"{x}"); /* Console.Out is a TextWriter */
You write data to the standard error stream using
System.Console.Error.Write
method,
like this,
double x = 1.23; System.Console.Error.WriteLine($"{x}"); /* or */ System.IO.TextWriter /* or just var */ stderr = System.Console.Error; stderr.WriteLine($"{x}");
The standard output of a program can be redirected into a file
myfile
with the directives
>myfile
(overwrite)
and
>>myfile
(append).
For example, the shell command
mono hello.exe > out.txtdeletes the contents of the file
out.txt
and then sends
the standard output of the program hello.exe
into that file.
Alternatively, the shell command
mono hello.exe >> out.txtappends the file
out.txt
with the standard output of
hello.exe
.
This two redirections are actually abbreviations of the full directives
1>file
and 1>>file
where 1 is
the default number of the stdout stream.
Analogously the standard error stream of a program can be redirected
into a file file
by the directives 2>file
or 2>>file
. For example, the following command
redirects standard output into out.txt
and standard error
into log.txt
.
mono prog.exe 1> out.txt 2> log.txt
&>file
redirects both stdout and stdin
into file
.
double x = 1.23; var outfile = new System.IO.StreamWriter("out.txt",append:true); outfile.WriteLine($"{x}"); outfile.Close(); /* do not forget this! */
System.Console.In
, automatically opened and connected to
the keyboard of the terminal (console) from which the program is run
(but can be conveniently redirected).
You can read from the stream using the
System.Console.In.ReadLine
method (can be also called as
System.Console.ReadLine
)
string s; double x; s = System.Console.In.ReadLine(); /* or s = System.Console.ReadLine(); */ x = double.Parse(s); /* or */ System.IO.TextReader /* or just var */ stdin = System.Console.In; s = stdin.ReadLine(); x = double.Parse(s);The standard input stream of a program can be attached to a
file
with the directive <file
, for example the
shell command
mono prog.exe < input.txtattaches the standard input stream of the program
prog.exe
with the file input.txt
such that all reads from the standard
input actually read from the attached file.
|
, connects the standard output of one program
to the standard input of another program, for example,
echo 1.23 | mono prog.exesends the string "1.23" into the standard input of the program
prog.exe
;
cat input.txt | mono prog.exesends the file
input.txt
into the standard input of the program
prog.exe
;
static void Main(string[] args){/* do stuff */}where
args
is the array of strings
that holds the command-line arguments, for example
static int Main(string[] args){ if(args.Length==0) { System.Console.Error.Write("there was no argument\n"); return 1; /* uups, error during execution */ } else { double x = double.Parse(args[0]); System.Console.Out.Write($"x = {x}\n"); return 0; } }
When the program is run the operating system calls the Main
function and provides its arguments as the array of blank-separated
strings taken from the command that ran the program,
mono prog.exe 1.23
Apart from stdin you can also open your own file streams for reading, for example,
var my_text_reader = new System.IO.StreamReader("input.txt"); string s = my_text_reader.ReadLine(); double x = double.Parse(s); infile.Close();