Reading/writing 2d string array from/to file
I can write a 1-d string array to file with:
Dim test() As String = {1, 2, 3, 4, 5}
File.WriteAllLines("C:\MyFile.txt", test)
but can't see how to write a 2-d array to file.
I've tried this:
Dim test(,) As String = {{1, 2, 3, 4, 5}, {34, 22, 12, 33, 55}}
Dim myfile = File.CreateText("C:\MyFile.txt")
Dim row, col As Integer
For row = 0 To 1
For col = 0 To 4
myfile.WriteLine(test(row, col))
Next
Next
which produces a file of 0 bytes.
What should I be doing?
Re: Reading/writing 2d string array from/to file
Try using the System.IO.StreamWriter class instead.
Re: Reading/writing 2d string array from/to file
try this:
vb Code:
Dim test(,) As String = {{1, 2, 3, 4, 5}, {34, 22, 12, 33, 55}}
IO.File.WriteAllLines("test.txt", Enumerable.Range(0, test.GetLength(0)).Select(Function(i1) String.Join(",", Enumerable.Range(0, test.GetLength(1)).Select(Function(i2) test(i1, i2)).ToArray)).ToArray)
i've got an example for reading a 2d array somewhere. i'll have a look for it.
Re: Reading/writing 2d string array from/to file
Re: Reading/writing 2d string array from/to file
Quote:
Originally Posted by
.paul.
try this:
vb Code:
Dim test(,) As String = {{1, 2, 3, 4, 5}, {34, 22, 12, 33, 55}}
IO.File.WriteAllLines("test.txt", Enumerable.Range(0, test.GetLength(0)).Select(Function(i1) String.Join(",", Enumerable.Range(0, test.GetLength(1)).Select(Function(i2) test(i1, i2)).ToArray)).ToArray)
i've got an example for reading a 2d array somewhere. i'll have a look for it.
Many thanks. I'm not sure I understand the code (!) but it works well.