Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Leave Comments, Critiques, and Suggestions Here?

Simple Input/Output with Tango

Input, as well as output, are essential parts of almost every program. Tango provides a wide set of tools for a wide set of issues.

This tutorial refers to the usage of the Tango package (See trunk/tango/io). If you're unfamiliar with conduits and other IO principles, please see the Chapter about IO in the Tango Reference Manual

Console I/O

While most programs may be graphical these days, console programs are still important...

Console Output

The simple way to do console output is via the Console package. In order to use it, import This allows one to write a string directly to the standard output device, which is usually the console. The following is a minimal Hello World! program in Tango:


void	main()
   Cout("Hello, World").newline;

Note that the newline was needed not only to write a new line to the console, but also to flush the buffer. Without the newline nothing would have printed (though you could have used flush instead of newline). Cout() returns an instance of Cout, so you can chain arguments together thus:

Cout("Hello")(" ")("World").newline;

(n.b. without a following set of parenthesis, newline ends the chaining process. This is an artifact of the compiler, rather than of Cout itself)

The Standard Error Stream: Note that there is a process analogous to Cout called Cerr. Use it in the same way. Do not expect it to be unbuffered.

Console Input

The next simplest program asks the user for some information. This is a "What's your name?" program:


void	main()
   char []	str;

   Cout("Hi There!").newline;
   Cout(" What's your name? ").flush;

   str = Cin.get();

   Cout("Well, hello, ")(str).newline;

The important thing here is the Cin object. In order to read a line into a string the get() method is used.

File I/O

There are several ways to talk to a file, or let a file talk to your program. If you are interested in using files for logging, please see the Logging Tutorial.

Detailed File Information

File Creation

File Modification

File Conduit