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
262 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now