locked
Storing values from a file permanently so that the file is not necessary once compiled

    Question

  • I need to read through a file and assign values from the file into an array that stores the values permanently once the program is compiled.  I am creating a program that will have multiple users and I don't want to send an accessory file each time I send the program out.  I could hardcode the values into an array, but that would be a lot of time and lots of lines of code. Below is a shortened version of the file format:

       

       A        B      C

        1        1       0
           1       2       0
           1       3       0
           1       4       0
           1       5       0
           1       6       1
           2       1       0
           2       2       0
           2       3       0
           2       4       1
           2       5       0

         2       6       0

     

    The array would be categorically dimensioned as Array(A,B,C), true dimensions of Array(0 to 1, 0 to 5, 0 to 1).  If the value of column C is 1 then 1 is stored in the array at the corresponding location, otherwise 0 is stored.  Once I have read through the file, I need to store this data permanently so that the next user doesn't need this file.  Anyone have any ideas on how to do this?  If I can't do this with an array, are there any other options?

     

    Thanks for any suggestions
          

    Monday, May 07, 2007 6:55 PM

Answers

  • Are you saying that these values will not change once the file is compiled?

     

    If so, you could put the file in your executable as a resource, then on application start-up extract the file and read the settings as appropriate. You would get the resource as a memorystream, then use some kind of TextReader (StreamReader has convenient methods) to read the lines from the file.

     

    Monday, May 07, 2007 8:27 PM
    Moderator
  • Off the top of my head, you could do something like this:

     

    1. Create a Memory Stream from the resource

    2. Create a text based stream reader using the underlying memory stream.

    3. Read each line from the stream reader, splitting the data out as necessary (e.g. string.split)

     

    Code Snippet

    Dim ms As New IO.MemoryStream(My.Resources.mine)

    Dim sr As New IO.StreamReader(ms)

    While sr.EndOfStream = False

        Dim line As String = sr.ReadLine

        Debug.WriteLine(line)

    End While

     

     

    Tuesday, May 08, 2007 11:31 AM
    Moderator

All replies

  •  RJBriscoe wrote:

    I need to read through a file and assign values from the file into an array that stores the values permanently once the program is compiled.  I am creating a program that will have multiple users and I don't want to send an accessory file each time I send the program out.  I could hardcode the values into an array, but that would be a lot of time and lots of lines of code. Below is a shortened version of the file format:

       

       A        B      C

        1        1       0
           1       2       0
           1       3       0
           1       4       0
           1       5       0
           1       6       1
           2       1       0
           2       2       0
           2       3       0
           2       4       1
           2       5       0

         2       6       0

     

    The array would be categorically dimensioned as Array(A,B,C), true dimensions of Array(0 to 1, 0 to 5, 0 to 1).  If the value of column C is 1 then 1 is stored in the array at the corresponding locationotherwise 0 is stored.  Once I have read through the file, I need to store this data permanently so that the next user doesn't need this file.  Anyone have any ideas on how to do this?  If I can't do this with an array, are there any other options?

     

    Thanks for any suggestions
          

     

     

    Hi,

     

    With 0 to 1 , 0 to 5 , 0 to 1 you have 2 * 6 * 2 = 24 distinct variations;

     

    • 0,0,0
    • 0,0,1
    • 1,0,0
    • 1,0,1
    • 0,1,0
    • 0,2,0
    • 0,3,0
    • 0,4,0
    • 0,5,0
    • 0,1,1
    • 0,2,1
    • 0,3,1
    • 0,4,1
    • 0,5,1
    • 1,1,0
    • 1,2,0
    • 1,3,0
    • 1,4,0
    • 1,5,0
    • 1,1,1
    • 1,2,1
    • 1,3,1
    • 1,4,1
    • 1,5,1

     I wouldn't say 24 separate variations is a lot myself. If you had a 100 or more then yes i'd say you have a lot.

     

    The only two methods of "permanent storage", as you put it, are writing and reading the values from a file or "hard-coding" these 24 variations into your application - program - solution - project, as far as i know.

     

    It would not take lots of lines of code to put these values into an array. >>

     

    Code Snippet

     

    Dim a, b, c As Integer

    Dim myArray(1, 5, 1) As Integer

     

    Private Sub Form1_Load(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles MyBase.Load

     

    For a = 0 To 1

    For b = 0 To 5

    For c = 0 To 1

    If c = 1 Then

    myArray(a, b, c) = 1

    Else

    myArray(a, b, c) = 0

    End If

    Next c

    Next b

    Next a

     

    End Sub

    'Done, easy eh?

    ' I've done it as above so you can use the

    ' values in other Functions or Subs ( subroutines ).

     

     

     

     

     

    Regards,

     

    S_DS

     

     

    Monday, May 07, 2007 8:25 PM
  • Are you saying that these values will not change once the file is compiled?

     

    If so, you could put the file in your executable as a resource, then on application start-up extract the file and read the settings as appropriate. You would get the resource as a memorystream, then use some kind of TextReader (StreamReader has convenient methods) to read the lines from the file.

     

    Monday, May 07, 2007 8:27 PM
    Moderator
  • I realize now that I explained it wrong.  My array will actually be two dimensions Array(a,b).  I want to read each row of data from the file and if column c is 0 for a particular row then the corresponding array value will be 0, but if column c is 1 then the array value will be 1.  For example, the first six values in my array using my values above would be:

    array(0,0) = 0

    array(0,1) = 0

    array(0,2) = 0

    array(0,3) = 0

    array(0,4) = 0

    array(0,5) = 1

     

    I also provided a shortened format of my file.  The actual dimensions of the array would be array(0 to 29, 0 to 24), so there are a lot of possible combinations.

     

    I know how to read from the file and assign to an array as described above.  My question is actually what the second replier is getting at.  The values will not change so I want them to be included in the executable without having to send an extra file to each user.

     

    Thanks for the suggestions

    Monday, May 07, 2007 9:57 PM
  •  RJBriscoe wrote:

    I realize now that I explained it wrong.  My array will actually be two dimensions Array(a,b).  I want to read each row of data from the file and if column c is 0 for a particular row then the corresponding array value will be 0, but if column c is 1 then the array value will be 1.  For example, the first six values in my array using my values above would be:

    array(0,0) = 0

    array(0,1) = 0

    array(0,2) = 0

    array(0,3) = 0

    array(0,4) = 0

    array(0,5) = 1

     

    I also provided a shortened format of my file.  The actual dimensions of the array would be array(0 to 29, 0 to 24), so there are a lot of possible combinations.

     

    I know how to read from the file and assign to an array as described above.  My question is actually what the second replier is getting at.  The values will not change so I want them to be included in the executable without having to send an extra file to each user.

     

    Thanks for the suggestions

     

    Hi,

     

    Are the number 1's ever going to change to a zero or vice-versa like they might in a database?

     

    Now you have 0 to 29, 0 to 24 then 30 * 25 = 750 variations. Quite a difference.

     

    Are you using a datagrid to show these three columns?

     

     

     

    Regards,

     

    S_DS

     

    Monday, May 07, 2007 10:11 PM
  • The 1's and 0's will never change.

     

    I am not showing these columns, I will loop through the array and if it is a 1 then specific code will be processed, if it is a 0 then different code will be processed.

    Monday, May 07, 2007 10:27 PM
  • You are correct, this is what I am saying.

     

    I have added the file as a resource, but I can't figure how to open the file and read the text from the file into a variable or an array.

     

    Here is what I have:

    Dim Termfile As UnmanagedMemoryStream

    Termfile = My.Resources.ResourceManager.GetStream("TERMFIS2.TRM")

     

    After running, Termfile = nothing.  Can you help me with the language on this?

     

    Thanks in advance

    Monday, May 07, 2007 11:41 PM
  • Off the top of my head, you could do something like this:

     

    1. Create a Memory Stream from the resource

    2. Create a text based stream reader using the underlying memory stream.

    3. Read each line from the stream reader, splitting the data out as necessary (e.g. string.split)

     

    Code Snippet

    Dim ms As New IO.MemoryStream(My.Resources.mine)

    Dim sr As New IO.StreamReader(ms)

    While sr.EndOfStream = False

        Dim line As String = sr.ReadLine

        Debug.WriteLine(line)

    End While

     

     

    Tuesday, May 08, 2007 11:31 AM
    Moderator