Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ExtraTopLevelOperators.fprintfn<'T> Function (F#)

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

textWriter

Type: TextWriter

format

Type: TextWriterFormat<'T>

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.

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


Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

Community Additions

ADD
Show:
© 2015 Microsoft