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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

622 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