You write data to the standard output stream like this,
double x = 1.23; System.Console.WriteLine("{0}",x); /* or */ System.Console.Out.WriteLine("{0}",x); /* or */ System.IO.TextWriter stdout = System.Console.Out; stdout.WriteLine("{0}",x);
You write data to the standard error stream like this,
double x = 1.23; System.Console.Error.WriteLine("{0}",x); /* or */ System.IO.TextWriter stderror = System.Console.Error; stderr.WriteLine("{0}",x);
The standard output of a program can be redirected into a file
myfile
with the directives
>myfile
and
>>myfile
.
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 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 an abbreviation 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
and 2>>file
. For example, the following command
redirects standard output into out.txt
and standard error
into errors.txt
.
mono prog.exe 1> out.txt 2> errors.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("{0}",x); outfile.Close();
System.Console.In
, automatically opened and connected to the terminal (console)
from which the program is run (but can be conveniently redirected).
You can read from the stream using the ReadLine
method,
System.IO.TextReader stdin = System.Console.In; string s = stdin.ReadLine(); double 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.
The pipe, |
, 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
;
Apart from stdin you can also open your own file streams for reading, for example,
var infile = new System.IO.StreamReader("input.txt"); string s = infile.ReadLine(); double x = double.Parse(s);
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) { Console.Error.Write("there was no argument\n"); return 1; } else { double x = double.Parse(args[0]); Console.Write("x = {0}\n",x); 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