Writing numbers to file and reading numbers from file

realolman

Member
Joined
Jan 2, 2017
Messages
7
Programming Experience
10+
I have "real world" digital inputs from a LabJack U12. I would like to keep track of when each of them occur, and for how long.
I have no trouble with the LabJack.... this is strictly a VB write to file and read from file issue...because I don't know how to do it


What I am looking for is how can I write 3 values for each on/off event to a file and then read the three values back and keep track of all the values, so that I can use them for calculations.

I have found examples that write a few things to a file and then read them back... and that works, but then I can't figure out how to iterate through the entire file of multiple event "writings" . I am an old geezer and unfamiliar with the methods used today

I like how this works but I can't figure out how to "read it back, three at a time... At this point the "f" and "c" are just experimental placeholders.

Every on / off event from the LabJack U12 would append to the end of the file with three numbers , a start DateTime, an end DateTime, and a duration.... I don't know how many events there might be in a given time period .

how can i read this back from a file, three at a time and iterate through until I have read them all ?

VB.NET:
       'StartTime was filled last time to this sub
   
        EndTime = Date.Now

        Dim duration As TimeSpan = EndTime.Subtract(StartTime)
        Dim c As String = "Abc13"
        Dim f As Single = 23.45

        dblDuration = duration.TotalSeconds       'using seconds, this can be integer    TotalSeconds is double
  
        sngDuration = dblDuration
 
        My.Computer.FileSystem.CurrentDirectory = "C:\Users\Realolman\Desktop"

        Dim fs As New FileStream("binary.txt", FileMode.Append, FileAccess.Write)
        Dim bwriter As New BinaryWriter(fs)

        bwriter.Write(c)
        bwriter.Write(dblDuration)
        bwriter.Write(f)

        bwriter.Close()
        fs.Close()
     
        StartTime = Date.Now

    End Sub

Thank you
 
Last edited by a moderator:
First things first, please don't post code with loads of pointless apace in it. You're just making it harder to read for no reason. Well-written code should rarely, if ever, have multiple blank lines together. If you like your code that way then please remove them when pasting code here. Please do all you can to help us help you, which includes making your code as readable as possible. I have edited it on this occasion.
 
As for the question, if you're writing data using a BinatyWriter then you need to read the data using a BinaryReader. If you know that data is being written in blocks of String, Double, Single then you need to read the data in blocks of String, Double, Single. The code would be virtually the same: you create a FileStream with the appropriate attributes, create a BinaryReader on top of that and then call three methods to read the three values. The BinaryReader has a dedicated method for reading data of each type. The PeekChar method of the BinaryReader can tell you when you've reached the end of the data.
 
With regards to the code you have, it's not a great idea to change the current directory like that. It would be better to specify the absolute path of the file and leave the current directory alone. Also, if you want to write to the desktop then you should probably be doing so for whatever user is currently logged in rather than a hard-coded user. You can get the path of that desktop folder and various other special folders from My.Computer.FileSystem.SpecialDirectories.
 
You have already recognized the low level of "expertise" you are dealing with with me. I do this as a hobbyist.... That is the reason for the hard coded path... so I can find the files easily to try to determine what is wrong .
I will certainly try your other suggestions.
Thank you for your help .. It seems I may have something going now
 
Is this the way to use the PeekChar?
Do I need to put it before every read? or something else entirely? So far I have been careful to always write a multiple of 3 items to the file, but that might not always be the case. I have the try exceptions commented out so I can tell what is going on
thank you

VB.NET:
  My.Computer.FileSystem.CurrentDirectory = "C:\Users\Wayne\Desktop"
        Dim fsr As New FileStream("binary.txt", FileMode.Open, FileAccess.Read)
        Dim breader As New BinaryReader(fsr)

        'Try
        'Catch ex As IOException
        Do While breader.PeekChar > -1
             x = breader.ReadInt32()
            str = breader.ReadString()
            f = breader.ReadSingle()
            Console.Write(x)
            Console.Write(vbCrLf)
            Console.Write(str)
            Console.Write(vbCrLf)
            Console.Write(f)
            Console.Write(vbCrLf)
            ' Console.WriteLine(ex.Message + "\n Cannot read from file.")
        Loop
            Return
        ' End Try
        breader.Close()
        fsr.Close()
 
That looks pretty good. PeekChar will tell you whether there's more data and, if there is, you know that there will be at least one record available, so you read the next record. Rinse and repeat.

Note that this:
VB.NET:
Console.Write(x)
Console.Write(vbCrLf)
does the same as this:
VB.NET:
Console.WriteLine(x)
If you ever want just a line break with no additional text, do this:
VB.NET:
Console.WriteLine()
If you wanted to, you could also replace all those calls with just one:
VB.NET:
Console.WriteLine($"{x}{Environment.NewLine}{str}{Environment.NewLine}{f}")
 
Thank you very much.
I have been trying to figure this out for at least 2 weeks... I could find bits and pieces, but I could never put it all together.
 
Back
Top