How to resolve the algorithm Read a specific line from a file step by step in the F# programming language
How to resolve the algorithm Read a specific line from a file step by step in the F# programming language
Table of Contents
Problem Statement
Some languages have special semantics for obtaining a known line number from a file.
Demonstrate how to obtain the contents of a specific line within a file. For the purpose of this task demonstrate how the contents of the seventh line of a file can be obtained, and store it in a variable or in memory (for potential future use within the program if the code were to become embedded). If the file does not contain seven lines, or the seventh line is empty, or too big to be retrieved, output an appropriate message. If no special semantics are available for obtaining the required line, it is permissible to read line by line. Note that empty lines are considered and should still be counted. Also note that for functional languages or languages without variables or storage, it is permissible to output the extracted data to standard output.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Read a specific line from a file step by step in the F# programming language
Source code in the fsharp programming language
open System
open System.IO
[<EntryPoint>]
let main args =
let n = Int32.Parse(args.[1]) - 1
use r = new StreamReader(args.[0])
let lines = Seq.unfold (
fun (reader : StreamReader) ->
if (reader.EndOfStream) then None
else Some(reader.ReadLine(), reader)) r
let line = Seq.nth n lines // Seq.nth throws an ArgumentException,
// if not not enough lines available
Console.WriteLine(line)
0
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the Stata programming language
You may also check:How to resolve the algorithm Deal cards for FreeCell step by step in the C++ programming language
You may also check:How to resolve the algorithm HTTP step by step in the GML programming language
You may also check:How to resolve the algorithm Table creation/Postal addresses step by step in the SAS programming language
You may also check:How to resolve the algorithm Almost prime step by step in the Delphi programming language