|
-
May 14th, 2013, 02:05 AM
#1
Thread Starter
New Member
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
-
May 14th, 2013, 02:06 AM
#2
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.
-
May 14th, 2013, 02:26 AM
#3
Thread Starter
New Member
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.
-
May 14th, 2013, 04:24 AM
#4
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?
-
May 14th, 2013, 04:30 AM
#5
Thread Starter
New Member
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.
-
May 14th, 2013, 05:07 AM
#6
Re: VS 2012: N00bie - Reading/writing an array from txt file?
 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.
 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?
-
May 14th, 2013, 05:13 AM
#7
Thread Starter
New Member
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Agreed 2D would be better.
-
May 14th, 2013, 08:00 AM
#8
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.
-
May 14th, 2013, 08:29 AM
#9
Junior Member
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Do you want to accomplish it using dynamc array or static array ?
-
May 14th, 2013, 08:59 AM
#10
Re: VS 2012: N00bie - Reading/writing an array from txt file?
 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.
-
May 14th, 2013, 11:38 AM
#11
Thread Starter
New Member
Re: VS 2012: N00bie - Reading/writing an array from txt file?
Cheers guys thanks. I'll give this a whirl.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|