VS 2012: N00bie - Reading/writing an array from txt file?
New to all this so bear with me. Haven't done any serious programming since the days of the spectrum zx81.
I have a file "numbers.txt" which looks like this...
0 1 4
2 6 3
I want to read this file into my app as...
a(0)=0, a(1)=1... a(5)=3
Also how would I do the reverse. i.e. write an array to file?
Thanks
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Are there always going to be six numbers in the file? If not, will you always know the numbers of values ahead of time? Are you always going to want to write the data to the same number of lines? You've given us an example but not told us what the rules are.
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Yes I will know how many numbers in advance.
A lot more than in the example. I am merely using 6 here and will expand when I write.
Re: VS 2012: N00bie - Reading/writing an array from txt file?
You answered my first question and your answer to that made my second question moot. Are you planning on answering my third question any time soon?
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Didn't realise it was this complicated.
I am going to need to read and write groups of numbers. Sometimes 5x5, 6x6, 7x7... 24x24 etc. Sometimes a few columns sometimes a lot.
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Quote:
Originally Posted by
tedski_69
Didn't realise it was this complicated.
You can't give a computer vague instructions. You have to tell it EXACTLY what to do. If we're going to tell you exactly what to tell the computer then we need to know exactly what you want it to do, not just one example with no idea in which direction to extrapolate.
Quote:
Originally Posted by
tedski_69
I am going to need to read and write groups of numbers. Sometimes 5x5, 6x6, 7x7... 24x24 etc. Sometimes a few columns sometimes a lot.
Would it not be more appropriate to use a 2D array given that your data seems to be stored in that format?
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Agreed 2D would be better.
Re: VS 2012: N00bie - Reading/writing an array from txt file?
E.g.
vb.net Code:
Private Function ReadMatrixFromFile(rowCount As Integer, columnCount As Integer, filePath As String) As Integer(,)
Dim matrix(rowCount, columnCount) As Integer
Using reader As New StreamReader(filePath)
For rowIndex = 0 To matrix.GetUpperBound(0)
'Read a line and split it on the spaces.
Dim values = reader.ReadLine().Split()
For columnIndex = 0 To matrix.GetUpperBound(1)
matrix(rowIndex, columnIndex) = CInt(values(columnIndex))
Next
Next
End Using
Return matrix
End Function
Private Sub WriteMatrixToFile(matrix As Integer(,), filePath As String)
Using writer As New StreamWriter(filePath)
For rowIndex = 0 To matrix.GetUpperBound(0)
'Write a line break before all but the first line.
If rowIndex > 0 Then
writer.WriteLine()
End If
For columnIndex = 0 To matrix.GetUpperBound(1)
'Write a space before all but the first value.
If columnIndex > 0 Then
writer.Write(" ")
End If
writer.Write(matrix(rowIndex, columnIndex).ToString())
Next
Next
End Using
End Sub
That code has no error handling, which should be at the level above.
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Do you want to accomplish it using dynamc array or static array ?
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Quote:
Originally Posted by
annaharris
Do you want to accomplish it using dynamc array or static array ?
In the truest sense, all arrays in .Net are static. Dynamic arrays are only a trick and a performance costly one at that. If one needs dynamic arrays, then one should be using a Collection(Of T) or a List(Of T) instead.
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Cheers guys thanks. I'll give this a whirl. :)