←to practical programming

Note "input/output"

Output

You can output data from your C#-program by writing to
  1. The standard output stream;
  2. The standard error stream;
  3. A file stream.

Standard output and standard error streams

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

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 (overwrite) and >>myfile (append). 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.exe 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 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

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 write specifically into 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($"{x}");
outfile.Close(); /* do not forget this! */

Input

You can input data into your (console) C#-program by
  1. Standard input stream;
  2. Command-line arguments;
  3. Input file stream.

Standard input stream

Every (console) C#-program has the standard input stream, 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.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.

Piping

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;

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) {
		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

Input file stream

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();