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
276 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
  • 2
  • 2
5 Comments
 
LVL 85

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 85

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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