F# - Basic I/O

Basic Input Output includes −
Reading from and writing into console.
Reading from and writing into file.
We have used the printf and the printfn functions for writing into the console. In this section, we will look into the details of the Printf module of F#.
Apart from the above functions, the Core.Printf module of F# has various other methods for printing and formatting using % markers as placeholders. The following table shows the methods with brief description −
Format specifications are used for formatting the input or output, according to the programmers’ need.
These are strings with % markers indicating format placeholders.
The syntax of a Format placeholders is −
%[flags][width][.precision][type]

The type is interpreted as −
A general format specifier, requires two arguments. The first argument is a function which accepts two arguments: first, a context parameter of the appropriate type for the given formatting function (for example, a TextWriter), and second, a value to print and which either outputs or returns appropriate text.
The second argument is the particular value to print.
The width is an optional parameter. It is an integer that indicates the minimal width of the result. For example, %5d prints an integer with at least spaces of 5 characters.
Valid flags are described in the following table −
printf "Hello "
printf "World"
printfn ""
printfn "Hello "
printfn "World"
printf "Hi, I'm %s and I'm a %s" "Rohit" "Medical Student"

printfn "d: %f" 212.098f
printfn "e: %f" 504.768f

printfn "x: %g" 212.098f
printfn "y: %g" 504.768f

printfn "x: %e" 212.098f
printfn "y: %e" 504.768f
printfn "True: %b" true
When you compile and execute the program, it yields the following output −
Hello World
Hello
World
Hi, I'm Rohit and I'm a Medical Studentd: 212.098000
e: 504.768000
x: 212.098
y: 504.768
x: 2.120980e+002
y: 5.047680e+002
True: true

This class is a part of the .NET framework. It represents the standard input, output, and error streams for console applications.
It provides various methods for reading from and writing into the console. The following table shows the methods −
The following example demonstrates reading from console and writing into it −
open System
let main() =
   Console.Write("What's your name? ")
   let name = Console.ReadLine()
   Console.Write("Hello, {0}\n", name)
   Console.WriteLine(System.String.Format("Big Greetings from {0} and {1}", "TutorialsPoint", "Absoulte Classes"))
   Console.WriteLine(System.String.Format("|{0:yyyy-MMM-dd}|", System.DateTime.Now))
main()
When you compile and execute the program, it yields the following output −
What's your name? Kabir
Hello, Kabir
Big Greetings from TutorialsPoint and Absoulte Classes
|2015-Jan-05|

The System.IO namespace contains a variety of useful classes for performing basic I/O.
It contains types or classes that allow reading and writing to files and data streams and types that provide basic file and directory support.
Classes useful for working with the file system −
The System.IO.File class is used for creating, appending, and deleting files.
System.IO.Directory class is used for creating, moving, and deleting directories.
System.IO.Path class performs operations on strings, which represent file paths.
System.IO.FileSystemWatcher class allows users to listen to a directory for changes.
Classes useful for working with the streams (sequence of bytes) −
System.IO.StreamReader class is used to read characters from a stream.
System.IO.StreamWriter class is used to write characters to a stream.
System.IO.MemoryStream class creates an in-memory stream of bytes.
The following table shows all the classes provided in the namespace along with a brief description −
The following example creates a file called test.txt, writes a message there, reads the text from the file and prints it on the console.
Note −  The amount of code needed to do this is surprisingly less!
open System.IO // Name spaces can be opened just as modules
File.WriteAllText("test.txt", "Hello There\n Welcome to:\n Tutorials Point")
let msg = File.ReadAllText("test.txt")
printfn "%s" msg
When you compile and execute the program, it yields the following output −
Hello There
Welcome to:
Tutorials Point

 Print
 Add Notes
 Bookmark this page