F Sharp Programming/Input and Output

Input and output, also called I/O, refers to any kind of communication between two hardware devices or between the user and the computer. This includes printing text out to the console, reading and writing files to disk, sending data over a socket, sending data to the printer, and a wide variety of other common tasks.

This page is not intended to provide an exhaustive look at .NET's I/O methods (readers seeking exhaustive references are encouraged to review the excellent documentation on the System.IO namespace on MSDN). This page will provide a cursory overview of some of the basic methods available to F# programmers for printing and working with files.

With F#
By now, you're probably familiar with the,  ,   and its variants in the Printf module. However, just to describe these methods more formally, these methods are used for printf-style printing and formatting using  markers as placeholders:

Print methods take a format string and a series of arguments, for example:

Methods in the  module are type-safe. For example, attempting to use substitute an  placeholder with a string results in a compilation error:

According to the F# documentation,  placeholders consist of the following:

[flags] (optional)

Valid flags are:
 * 0: add zeros instead of spaces to make up the required width
 * '-': left justify the result within the width specified
 * '+': add a '+' character if the number is positive (to match a '-' sign for negatives)
 * ' ': add an extra space if the number is positive (to match a '-' sign for negatives)

[width] (optional)

The optional width is an integer indicating the minimal width of the result. For instance, %6d prints an integer, prefixing it with spaces to fill at least 6 characters. If width is '*', then an extra integer argument is taken to specify the corresponding width.
 * any number

[.precision] (optional)

Represents the number of digits after a floating point number.

[type] (required)

The following placeholder types are interpreted as follows: %b:        bool, formatted as "true" or "false" %s:        string, formatted as its unescaped contents %d, %i:    any basic integer type formatted as a decimal integer, signed if the basic integer type is signed. %u:        any basic integer type formatted as an unsigned decimal integer %x, %X, %o: any basic integer type formatted as an unsigned hexadecimal (a-f)/Hexadecimal (A-F)/Octal integer %e, %E, %f, %F, %g, %G: any basic floating point type (float,float32) formatted using a C-style floating point format specifications, i.e    %e, %E: Signed value having the form [-]d.dddde[sign]ddd where d is a single decimal digit, dddd is one or more decimal digits, ddd is exactly three decimal digits, and sign is + or - %f:    Signed value having the form [-]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on                 the requested precision. %g, %G: Signed value printed in f or e format, whichever is                 more compact for the given value and precision. %M:     System.Decimal value %O:     Any value, printed by boxing the object and using it's ToString method(s) %A:     Any value, printed by using Microsoft.FSharp.Text.StructuredFormat.Display.any_to_string with the default layout settings %a:     A general format specifier, requires two arguments: (1) a function which accepts two arguments: (a) a context parameter of the appropriate type for the given formatting function (e.g. an #System.IO.TextWriter) (b) a value to print and which either outputs or returns appropriate text. (2) the particular value to print %t:     A general format specifier, requires one argument: (1) a function which accepts a context parameter of the appropriate type for the given formatting function (e.g.                     an #System.IO.TextWriter)and which either outputs or returns appropriate text.

Basic integer types are: byte,sbyte,int16,uint16,int32,uint32,int64,uint64,nativeint,unativeint Basic floating point types are: float, float32 Programmers can print to the console using the  method, however F# recommends reading console input using the   method.

With .NET
.NET includes its own notation for format specifiers. .NET format strings are untyped. Additionally, .NET's format strings are designed to be extensible, meaning that a programmer can implement their own custom format strings. Format placeholders have the following form:

For example, using the  method in fsi:

See Number Format Strings, Date and Time Format Strings, and Enum Format Strings for a comprehensive reference on format specifiers for .NET.

Programmers can read and write to the console using the System.Console class:

System.IO Namespace
The System.IO namespace contains a variety of useful classes for performing basic I/O.

Files and Directories
The following classes are useful for interrogating the host filesystem:
 * The System.IO.File class exposes several useful members for creating, appending, and deleting files.
 * System.IO.Directory exposes methods for creating, moving, and deleting directories.
 * System.IO.Path performs operations on strings which represent file paths.
 * System.IO.FileSystemWatcher which allows users to listen to a directory for changes.

Streams
A stream is a sequence of bytes. .NET provides some classes which allow programmers to work with streams including:


 * System.IO.StreamReader which is used to read characters from a stream.
 * System.IO.StreamWriter which is used to write characters to a stream.
 * System.IO.MemoryStream which creates an in-memory stream of bytes.