2

Having trouble with my code, I resort to writing the contents of my arrays to file, like this:-

Private Sub DumpArray (ByRef array_to_dump(,) As MyEnum, ByVal file_name As String)

    Dim sw As StreamWriter = New StreamWriter(file_name)

    For i As Integer = array_to_dump.GetLowerBound(0) To array_to_dump.GetUpperBound(0)

        For j As Integer = array_to_dump.GetLowerBound(1) To array_to_dump.GetUpperBound(1)

            If array_to_dump(i, j) = MyEnum.This Then
                sw.Write("Fred")
            Else
                sw.Write("Bert")
            End If

            sw.Write(vbTab)

        Next j

        sw.WriteLine

    Next i

    sw.Flush
    sw.Close

End Sub

This produces the output I expect; in the current case a file filled with "Fred", separated by tabs. However, if I change the business end of the code to this:-

            If array_to_dump(i, j) = MyEnum.This Then
                sw.Write("1")
            Else
                sw.Write("0")
            End If

the file is filled with non-printing characters, that come out as little boxes in Notepad, rather than the rows of "0" separated by tabs that I was expecting. Any other pairs of single-character strings do the same.

While not a matter of pressing importance, I am idly curious as to why this should be. Does anyone know?

Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
  • 1
    Have you tried setting the encoding of the streamwriter – David Sdot Apr 21 '15 at 09:52
  • @DavidSdot, no, I haven't touched it. I can't see what I should do with `Encoding`, which appears to be read-only in any case. Is there something I am missing here? – Brian Hooper Apr 21 '15 at 10:00
  • I cannot reproduce the problem with the code supplied. My file gets 0's and 1's written correctly. – SSS Apr 22 '15 at 06:50
  • Are you sure that when writing the 1 and 0 to the file, that you have wrapped them in quotes? The code here should work correctly. I could've sworn I saw this question before. The poster was trying to write single characters to a file but was using `sw.Write(1)` (without quotes), resulting in the file not being written correctly. – Chris Dunaway Apr 22 '15 at 14:56
  • It was [this question](http://stackoverflow.com/questions/29755324/binary-reading-of-bytes-returning-only-one-value-c-sharp) that I was thinking of. Not a duplicate, but might help. – Chris Dunaway Apr 22 '15 at 15:11
  • @ChrisDunaway, Thanks for the tip, but I definately had quotes round the values. My quick and dirty fix was to write `"00"` and `"01"` instead. – Brian Hooper Apr 22 '15 at 15:46

2 Answers2

1

You will have to set the encoding to something else when you declare your SteamWriter like @DavidSdot said. Like so:

Dim writer As StreamWriter = New StreamWriter(file_name, Encoding.Default)

Mess around with that property and you will probably find a good value. I'm no expert in encoding but this should be the culprit of your problem.

Viktor Ek
  • 353
  • 1
  • 13
1

I tried to repro the problem but the code below works correctly. Compare it to your real code... maybe there is a typo or something in the original?

Option Strict On
Imports System.IO

Public Class Form1
  Private Enum MyEnum As Integer
    This
    That
  End Enum

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim myArray(1, 1) As MyEnum
    myArray(0, 0) = MyEnum.This
    myArray(0, 1) = MyEnum.This
    myArray(1, 0) = MyEnum.This
    myArray(1, 1) = MyEnum.That

    Call DumpArray(myArray, "C:\Junk\Junk.txt")
  End Sub

  Private Sub DumpArray(ByRef array_to_dump(,) As MyEnum, ByVal file_name As String)

    Dim sw As StreamWriter = New StreamWriter(file_name)
    For i As Integer = array_to_dump.GetLowerBound(0) To array_to_dump.GetUpperBound(0)
      For j As Integer = array_to_dump.GetLowerBound(1) To array_to_dump.GetUpperBound(1)
        If array_to_dump(i, j) = MyEnum.This Then
          sw.Write("0")
        Else
          sw.Write("1")
        End If
        sw.Write(vbTab)
      Next j
      sw.WriteLine()
    Next i
    sw.Flush()
    sw.Close()
  End Sub
End Class
SSS
  • 4,807
  • 1
  • 23
  • 44