Read Txt, Streamreader

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stefan Krantz
    New Member
    • Feb 2012
    • 1

    Read Txt, Streamreader

    Hi,

    Iam a real newbee on VB.net. Have ued VB back in the days on version 3 and 4...

    Now I plan to do a little program that helps me get data from a txt file and then in to varibles...

    The text file look like this and contain many records, stores 3 fields, Temperature, date and time..

    5.2;2011-10-11 09:59:51
    5.3;2011-10-11 10:00:30
    5.3;2011-10-11 10:00:30
    5.4;2011-10-11 10:01:09
    5.4;2011-10-11 10:01:48
    5.4;2011-10-11 10:02:27
    5.4;2011-10-11 10:03:06

    Goal is to read this in to a array with 3 varibles.

    How could this be done?
    The fields in the text file are not sepatated same.. ; and space?

    Thanks in advance
    /Stefan
  • !NoItAll
    Contributor
    • May 2006
    • 297

    #2
    Either you should use two variables:
    Temp
    DateTime <--This is a valid variable in vb.net

    If you must have three variables then you can do it, but I don't like your data structure much because I don't like parsing out fields using spaces - it's just bad form.

    Create a structure
    Code:
    Structure TempAndDate_Type
        Dim Temp as String
        Dim DateAndTime as DateTime
    End Structure
    Create an instance of the structure
    Code:
    Dim MyFields as new TempAndDate_Type
    Now read up the file into an array
    Code:
    Dim MyText() as String = My.Computer.Filesystem.ReadAllLines("c:\myfile")
    So now you have a structure and an array containing the lines from the file and you need to parse out the elements from the string into the structure. I will stop here so you can exercise your brain. Look into the String.Split method to get the fields from each line into your structure. Hint: Parse on the semi-colon.

    Also - remember, there are lots of ways this can be done, but this reply only suggests one very simple way.

    Comment

    Working...