Results 1 to 6 of 6

Thread: variables in text files

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    42

    variables in text files

    Hi, could anyone point me in the direction of a tutorial on saving variables to a text file and then accessing this value at a later date. I know how to write a variable to a textfile using

    oWrite.WriteLine(variablename)
    oWrite.WriteLine(variablename2)

    etc

    but how do I read the value back into the variable from the textfile, and if there is more than one value in the text file how do I tell it which value to put into which variable...?

    The full code i used for writing the variables to the file is...

    ----------------------

    Dim oFile As System.IO.File
    Dim oWrite As System.IO.StreamWriter

    (on form load)
    oWrite = oFile.CreateText("C:\Program Files\Folder\filename.txt")

    (on button click)
    oWrite.WriteLine(variable1)
    oWrite.WriteLine(variable2)
    oWrite.Close()

    ----------------------

    I know how to open the file at a later date [using 'Dim oRead As System.IO.StreamReader' and 'oRead = oFile.OpenText("C:\Program Files\Folder\filename.txt")'], its everything after that I'm stuck on!

    Many thanks in advance for any help you can give and sorry if I don't make enough sense for you to help!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    You didn't say how your txt file look like . Is it delimited or you save each variable in a different line ?

    This is the basic way to read values into variables .
    VB Code:
    1. Dim reader As New IO.StreamReader("path")
    2.         Dim var1 As Integer = reader.ReadLine()
    3.         Dim var2 As Integer = reader.ReadLine()

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    42
    thats great. What if I don't know the variable line though... Is there a way of writing the variable name AND the value it holds to the textfile, then reading this back and entering it into the correct variable.

    say i have a variable called 'name' that holds the value "Ross", can i write both the variable name and value to the textfile ( in a format such as name = "Ross"), then read this back into the variable 'name'.

    Many thanks again for your help.

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Ok , that can be done but it would require a lot of code and not efficient way , if you ask me . What datatypes are you writing to the file ? I'm thinking you can declare an array of string or whatever , then you write a these values , separate them with separator , then it would be so easy to get them in the right order into the array . Tell me if you want a demo ?

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    42
    Originally posted by Pirate
    Ok , that can be done but it would require a lot of code and not efficient way , if you ask me . What datatypes are you writing to the file ? I'm thinking you can declare an array of string or whatever , then you write a these values , separate them with separator , then it would be so easy to get them in the right order into the array . Tell me if you want a demo ?
    A demo of this would be great if you have the time. I am using integers and strings. I am pretty new to this as you can tell!

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This is integral values stored in line by line .
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width