ExtraTopLevelOperators.fprintfn<'T> Function (F#)

The fprintfn prints to a file using the given format, and add a newline.

Namespace/Module Path: Microsoft.FSharp.Core.ExtraTopLevelOperators

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
fprintfn : TextWriter -> TextWriterFormat<'T> -> 'T

// Usage:
fprintfn textWriter format

Parameters

Remarks

This function is named PrintFormatLineToTextWriter in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

The following example demonstrates the use of fprintfn to print a listing of the contents of a directory to a specified file, directorylisting.txt.

let fileName = "directoryListing.txt" 
let printDirectoryInfo (dirName:string) (fileName:string) =
    use file = System.IO.File.CreateText(fileName)
    System.IO.Directory.EnumerateFileSystemEntries(dirName)
    |> Seq.iter (fun elem -> fprintfn file "%s" elem )
printDirectoryInfo @"C:\" fileName
printfn "%s" (System.IO.File.OpenText(fileName).ReadToEnd())

The following example is similar to the previous except that it also demonstrates the use of specifiers to customize the output in the format string. For more information on format specifiers, see Printf Module.

let fileName = "directoryListingXY.txt" 
let printDirectoryInfo (dirName:string) (fileName:string) =
    use file = System.IO.File.CreateText(fileName)
    System.IO.Directory.EnumerateDirectories(dirName)
    |> Seq.map (fun elem -> new System.IO.DirectoryInfo(elem))
    |> Seq.iter (fun elem -> fprintfn file "%50s %A" elem.FullName elem.LastAccessTime )
    System.IO.Directory.EnumerateFiles(dirName)
    |> Seq.map (fun elem -> new System.IO.FileInfo(elem))
    |> Seq.iter (fun elem -> fprintfn file "%50s %A" elem.FullName elem.LastAccessTime )
printDirectoryInfo @"C:\" fileName
printfn "%s" (System.IO.File.OpenText(fileName).ReadToEnd())

Platforms

Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Reference

Core.ExtraTopLevelOperators Module (F#)

Microsoft.FSharp.Core Namespace (F#)