Solved

Writing an array to a text file and also writing it back to the array in VB 2005

Posted on 2006-11-25
5
287 Views
Last Modified: 2008-02-01
Hi Everyone,

I would like to write an array (8 values) to a text file, so save the values. I would then like to be able to read the values and populate the array with the values in the text value. Using the text file as a place to save the array values during different sessions if you will.

I would welcome any help pages, or advice on where I should look on how to acheive this.

Thanks,
Adam
0
Comment
Question by:eadam-uk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18012136
What kind of Array are you dealing with?   Integer, String?...

If String, will there be any carriage returns in the values?
0
 
LVL 29

Accepted Solution

by:
Nightman earned 500 total points
ID: 18012169
Here's a very simple example:


  Private Sub BuildArrayAndTestReadWrite()
    Dim x(8) As String

    x(0) = "1"
    x(1) = "2"
    x(2) = "3"
    x(3) = "4"
    x(4) = "5"
    x(5) = "6"
    x(6) = "7"
    x(7) = "8"
    SaveArrayToFile(x)
    LoadArrayFromFile(x)


  End Sub

  Private Sub LoadArrayFromFile(ByRef x As Array)
    Dim sr As StreamReader
    Dim i As Integer
    sr = New StreamReader("SavedArray.txt")

    Do While sr.Peek() >= 0
      'This is an arbitrary size for this example.

      x(i) = sr.ReadLine()
      'The output will look odd, because
      'only five characters are read at a time.

      i = i + 1
    Loop
    sr.Close()

  End Sub

  Private Sub SaveArrayToFile(ByRef x As Array)
    Dim sw As StreamWriter = New StreamWriter("SavedArray.txt")
    Dim i As Integer
    For i = LBound(x) To UBound(x)
      sw.WriteLine(x(i))
    Next
    sw.Close()
  End Sub
0
 
LVL 29

Expert Comment

by:Nightman
ID: 18012173
You would have to use
Imports System.IO
in the class

Also - ignore the comments in the code (straight from the MSDN ;) - and adjusted slightly)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 18012407
You could do something like...

    Dim values() As String = {"a", "b", "c", "d", "e", "f", "g", "h"}

    ' To write the values...
    My.Computer.FileSystem.WriteAllText("C:\someFile.txt", String.Join("|", values), False)

    ' To read the values...
    values = My.Computer.FileSystem.ReadAllText("C:\someFile.txt").Split("|")

(given that "|" is not in the original strings...)
0
 

Author Comment

by:eadam-uk
ID: 18012906
Thanks nightman, worked a treat.

Sorry idle_mind I didnt see your final comment before I dished out the points, but thank you too!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help converting bitmap to image in VB.Net 8 69
vb.net convert long time to mm:ss 23 35
make control visible based off other control value 7 45
vb.net dbnull syntax 1 59
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question