File tree Expand file tree Collapse file tree 18 files changed +404
-0
lines changed
snippets/fsharp/System.IO/BinaryReader Expand file tree Collapse file tree 18 files changed +404
-0
lines changed Original file line number Diff line number Diff line change
1
+ <Project Sdk =" Microsoft.NET.Sdk" >
2
+ <PropertyGroup >
3
+ <OutputType >Exe</OutputType >
4
+ <TargetFramework >net6.0</TargetFramework >
5
+ </PropertyGroup >
6
+
7
+ <ItemGroup >
8
+ <Compile Include =" rwdouble.fs" />
9
+ </ItemGroup >
10
+ </Project >
Original file line number Diff line number Diff line change
1
+ // <Snippet1>
2
+ open System
3
+ open System.IO
4
+
5
+ let arrayLength = 1000
6
+
7
+ // Create random data to write to the stream.
8
+ let randomGenerator = Random()
9
+ let dataArray =
10
+ Array.init arrayLength ( fun _ -> 100.1 * randomGenerator.NextDouble())
11
+ do
12
+ use binWriter = new BinaryWriter( new MemoryStream())
13
+ // Write the data to the stream.
14
+ printfn $" Writing data to the stream."
15
+ for num in dataArray do
16
+ binWriter.Write num
17
+
18
+ // Create a reader using the stream from the writer.
19
+ use binReader = new BinaryReader( binWriter.BaseStream)
20
+ try
21
+ // Return to the beginning of the stream.
22
+ binReader.BaseStream.Position <- 0
23
+
24
+ // Read and verify the data.
25
+ printfn " Verifying the written data."
26
+ for num in dataArray do
27
+ if binReader.ReadDouble() <> num then
28
+ printfn " Error writing data."
29
+ printfn " The data was written and verified."
30
+ with :? EndOfStreamException as e ->
31
+ printfn $" Error writing data: {e.GetType().Name}."
32
+ // </Snippet1>
Original file line number Diff line number Diff line change
1
+ <Project Sdk =" Microsoft.NET.Sdk" >
2
+ <PropertyGroup >
3
+ <OutputType >Exe</OutputType >
4
+ <TargetFramework >net6.0</TargetFramework >
5
+ </PropertyGroup >
6
+
7
+ <ItemGroup >
8
+ <Compile Include =" source.fs" />
9
+ <Compile Include =" source5.fs" />
10
+ </ItemGroup >
11
+ </Project >
Original file line number Diff line number Diff line change
1
+ module source
2
+
3
+ // <Snippet1>
4
+ open System.IO
5
+ open System.Text
6
+
7
+ let fileName = " AppSettings.dat"
8
+
9
+ let writeDefaultValues () =
10
+ use stream = File.Open( fileName, FileMode.Create)
11
+ use writer = new BinaryWriter( stream, Encoding.UTF8, false )
12
+ writer.Write 1.250 F
13
+ writer.Write @" c:\Temp"
14
+ writer.Write 10
15
+ writer.Write true
16
+
17
+ let displayValues () =
18
+ if File.Exists fileName then
19
+ use stream = File.Open( fileName, FileMode.Open)
20
+ use reader = new BinaryReader( stream, Encoding.UTF8, false )
21
+ let aspectRatio = reader.ReadSingle()
22
+ let tempDirectory = reader.ReadString()
23
+ let autoSaveTime = reader.ReadInt32()
24
+ let showStatusBar = reader.ReadBoolean()
25
+
26
+ printfn $" Aspect ratio set to: {aspectRatio}"
27
+ printfn $" Temp directory is: {tempDirectory}"
28
+ printfn $" Auto save time set to: {autoSaveTime}"
29
+ printfn $" Show status bar: {showStatusBar}"
30
+
31
+ writeDefaultValues ()
32
+ displayValues ()
33
+ // </Snippet1>
Original file line number Diff line number Diff line change
1
+ module source5
2
+
3
+ //<snippet6>
4
+ open System
5
+ open System.IO
6
+ open System.Text
7
+
8
+ let CHUNK_SIZE = 1024
9
+
10
+ let dumpBytes ( bdata : byte []) len =
11
+ let mutable j = 0
12
+ // 3 * 16 chars for hex display, 16 chars for text and 8 chars
13
+ // for the 'gutter' int the middle.
14
+ let dumptext = StringBuilder( " " , 16 * 4 + 8 )
15
+ for i = 0 to len - 1 do
16
+ dumptext.Insert( j * 3 , $" {int bdata[i]:X2} " ) |> ignore
17
+ let dchar = char bdata[ i]
18
+ //' replace 'non-printable' chars with a '.'.
19
+ let dchar =
20
+ if Char.IsWhiteSpace dchar || Char.IsControl dchar then
21
+ '.'
22
+ else
23
+ dchar
24
+ dumptext.Append dchar |> ignore
25
+ j <- j + 1
26
+ if j = 16 then
27
+ printfn $" {dumptext}"
28
+ dumptext.Length <- 0
29
+ dumptext.Append " " |> ignore
30
+ j <- 0
31
+ // display the remaining line
32
+ if j > 0 then
33
+ for i = j to 15 do
34
+ dumptext.Insert( j * 3 , " " ) |> ignore
35
+ printfn $" {dumptext}"
36
+
37
+ [<EntryPoint>]
38
+ let main args =
39
+ if args.Length = 0 || File.Exists args[ 0 ] |> not then
40
+ printfn " Please provide an existing file name."
41
+ else
42
+ use fs = new FileStream( args[ 0 ], FileMode.Open, FileAccess.Read)
43
+ use br = new BinaryReader( fs, ASCIIEncoding())
44
+
45
+ let mutable chunk = br.ReadBytes CHUNK_ SIZE
46
+ while chunk.Length > 0 do
47
+ dumpBytes chunk chunk.Length
48
+ chunk <- br.ReadBytes CHUNK_ SIZE
49
+ 0
50
+ //</snippet6>
Original file line number Diff line number Diff line change
1
+ <Project Sdk =" Microsoft.NET.Sdk" >
2
+ <PropertyGroup >
3
+ <OutputType >Exe</OutputType >
4
+ <TargetFramework >net6.0</TargetFramework >
5
+ </PropertyGroup >
6
+
7
+ <ItemGroup >
8
+ <Compile Include =" rwreadchar.fs" />
9
+ <Compile Include =" rwreadbytes.fs" />
10
+ <Compile Include =" rwreadchars.fs" />
11
+ </ItemGroup >
12
+ </Project >
Original file line number Diff line number Diff line change
1
+ module rwreadbytes
2
+
3
+ // <Snippet1>
4
+ open System
5
+ open System.IO
6
+
7
+ let arrayLength = 1000
8
+ let dataArray = Array.zeroCreate< byte> arrayLength
9
+ let verifyArray = Array.zeroCreate< byte> arrayLength
10
+
11
+ Random() .NextBytes dataArray
12
+
13
+ do
14
+ use binWriter = new BinaryWriter( new MemoryStream())
15
+ printfn " Writing the data."
16
+ binWriter.Write( dataArray, 0 , arrayLength)
17
+
18
+ use binReader = new BinaryReader( binWriter.BaseStream)
19
+ binReader.BaseStream.Position <- 0
20
+
21
+ if binReader.Read( verifyArray, 0 , arrayLength) <> arrayLength then
22
+ printfn " Error writing the data."
23
+ else
24
+ for i = 0 to arrayLength - 1 do
25
+ if verifyArray[ i] <> dataArray[ i] then
26
+ printfn " Error writing the data."
27
+ else
28
+ printfn " The data was written and verified."
29
+ // </Snippet1>
Original file line number Diff line number Diff line change
1
+ module rwreadchar
2
+
3
+ // <Snippet1>
4
+ open System
5
+ open System.IO
6
+
7
+ let invalidPathChars = Path.GetInvalidPathChars()
8
+ let memStream = new MemoryStream()
9
+ let binWriter = new BinaryWriter( memStream)
10
+
11
+ // Write to memory.
12
+ printf " Invalid file path characters are: "
13
+ for i = 0 to invalidPathChars.Length - 1 do
14
+ binWriter.Write invalidPathChars[ i]
15
+
16
+ // Create the reader using the same MemoryStream
17
+ // as used with the writer.
18
+ let binReader = new BinaryReader( memStream)
19
+
20
+ // Set Position to the beginning of the stream.
21
+ memStream.Position <- 0
22
+
23
+ // Read the data from memory and write it to the console.
24
+ printf $" {binReader.ReadString()}"
25
+ let memoryData =
26
+ [| for _ = 0 L to memStream.Length - memStream.Position - 1 L do
27
+ Convert.ToChar( binReader.Read()) |]
28
+ printfn $" {memoryData}"
29
+ // </Snippet1>
Original file line number Diff line number Diff line change
1
+ // <Snippet1>
2
+ open System.IO
3
+
4
+ let invalidPathChars = Path.GetInvalidPathChars()
5
+ let memStream = new MemoryStream()
6
+ let binWriter = new BinaryWriter( memStream)
7
+
8
+ // Write to memory.
9
+ binWriter.Write " Invalid file path characters are: "
10
+ binWriter.Write( invalidPathChars, 0 , invalidPathChars.Length)
11
+
12
+ // Create the reader using the same MemoryStream
13
+ // as used with the writer.
14
+ let binReader = new BinaryReader( memStream)
15
+
16
+ // Set Position to the beginning of the stream.
17
+ memStream.Position <- 0
18
+
19
+ // Read the data from memory and write it to the console.
20
+ printf $" {binReader.ReadString()}"
21
+ let arraySize = memStream.Length - memStream.Position |> int
22
+ let memoryData = Array.zeroCreate< char> arraySize
23
+ binReader.Read( memoryData, 0 , arraySize) |> ignore
24
+ printfn $" {memoryData}"
25
+ // </Snippet1>
Original file line number Diff line number Diff line change
1
+ <Project Sdk =" Microsoft.NET.Sdk" >
2
+ <PropertyGroup >
3
+ <OutputType >Exe</OutputType >
4
+ <TargetFramework >net6.0</TargetFramework >
5
+ </PropertyGroup >
6
+
7
+ <ItemGroup >
8
+ <Compile Include =" rwbyte.fs" />
9
+ </ItemGroup >
10
+ </Project >
You can’t perform that action at this time.
0 commit comments