Results 1 to 11 of 11

Thread: VS 2012: N00bie - Reading/writing an array from txt file?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    5

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    5

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    5

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VS 2012: N00bie - Reading/writing an array from txt file?

    Quote Originally Posted by tedski_69 View Post
    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 View Post
    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    5

    Re: VS 2012: N00bie - Reading/writing an array from txt file?

    Agreed 2D would be better.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VS 2012: N00bie - Reading/writing an array from txt file?

    E.g.
    vb.net Code:
    1. Private Function ReadMatrixFromFile(rowCount As Integer, columnCount As Integer, filePath As String) As Integer(,)
    2.     Dim matrix(rowCount, columnCount) As Integer
    3.  
    4.     Using reader As New StreamReader(filePath)
    5.         For rowIndex = 0 To matrix.GetUpperBound(0)
    6.             'Read a line and split it on the spaces.
    7.             Dim values = reader.ReadLine().Split()
    8.  
    9.             For columnIndex = 0 To matrix.GetUpperBound(1)
    10.                 matrix(rowIndex, columnIndex) = CInt(values(columnIndex))
    11.             Next
    12.         Next
    13.     End Using
    14.  
    15.     Return matrix
    16. End Function
    17.  
    18. Private Sub WriteMatrixToFile(matrix As Integer(,), filePath As String)
    19.     Using writer As New StreamWriter(filePath)
    20.         For rowIndex = 0 To matrix.GetUpperBound(0)
    21.             'Write a line break before all but the first line.
    22.             If rowIndex > 0 Then
    23.                 writer.WriteLine()
    24.             End If
    25.  
    26.             For columnIndex = 0 To matrix.GetUpperBound(1)
    27.                 'Write a space before all but the first value.
    28.                 If columnIndex > 0 Then
    29.                     writer.Write(" ")
    30.                 End If
    31.  
    32.                 writer.Write(matrix(rowIndex, columnIndex).ToString())
    33.             Next
    34.         Next
    35.     End Using
    36. End Sub
    That code has no error handling, which should be at the level above.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    17

    Re: VS 2012: N00bie - Reading/writing an array from txt file?

    Do you want to accomplish it using dynamc array or static array ?

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: VS 2012: N00bie - Reading/writing an array from txt file?

    Quote Originally Posted by annaharris View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    5

    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
  •  



Click Here to Expand Forum to Full Width