←to practical programming

Note "input/output"

Output

You can output data from your C#-program by writing to i) standard output stream; ii) standard error stream; and iii) file stream.

Standard output and standard error streams

For every (console) C#-program there are always two output streams open: the standard output stream and the standard error stream. 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 shown on 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 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);

Redirection of standard output and standard error streams

A POSIX shell can disconnect the standard streams from their default devices and connect them to another devices. This is called redirection of standard streams.

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.txt
deletes 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.txt
appends 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

The directive &>file redirects both stdout and stdin into file.

File streams

Usually the two standard streams are enough for a program to output its data via redirection. However sometimes you need to wrie specifically to a file with a certain name. You do this by creating a stream connected with the given file,
double x = 1.23;
var outfile = new System.IO.StreamWriter("out.txt",append:true);
outfile.WriteLine("{0}",x);
outfile.Close();

Input

You can input data into your (console) C#-program by
  1. Command-line arguments;
  2. Reading from standard input;
  3. Reading from file streams.

Standard input stream and file streams

Every (console) C#-program has the standard input stream, 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.txt
attaches 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.exe
sends the string "1.23" into the standard input of the program prog.exe;
cat input.txt | mono prog.exe
sends 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);

Command-line arguments

A small amount of data can be passed to a console C#-program via command-line arguments by declaring the main function as
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