Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Read/Write a struct into a file

I have a struct in vb.net like:

structure test
dim element1 as string
dim element2 as integer
dim element3 as bool
end structure

dim varTest as test


I want to write and read the whole struct variable varTest
in a file.

I already try with fileput command like

fileput(freefile,binarymodeRead,varTest)
and it looks like it work

but when I try to read de struct with fileget
fileget(freefile,binarymodeWrite, varTest)
the app give me this error

Is there another way to read/write the whole struct into or from
a file.


Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    : I have a struct in vb.net like:
    :
    : structure test
    : dim element1 as string
    : dim element2 as integer
    : dim element3 as bool
    : end structure
    :
    : dim varTest as test
    :
    :
    : I want to write and read the whole struct variable varTest
    : in a file.
    :
    : I already try with fileput command like
    :
    : fileput(freefile,binarymodeRead,varTest)
    : and it looks like it work
    :
    : but when I try to read de struct with fileget
    : fileget(freefile,binarymodeWrite, varTest)
    : the app give me this error
    :
    : Is there another way to read/write the whole struct into or from
    : a file.
    :
    :
    :

    Use serialization. Import these namesapces like so:

    [code]
    Imports System.IO
    Imports System.Runtime.Serialization.Formatters.Binary
    [/code]

    Then create a File stream and pass it to a Binary Formatter like so ...

    [code]
    Dim fs As New FileStream("C:somefile.dat", FileMode.OpenOrCreate)
    Dim bf As New BinaryFormatter()
    Call bf.Serialize(fs, test)
    fs.Close()
    [/code]

    If u later need to retrieve the struct from the file then u use this code ...


    [code]
    Dim fs As New FileStream("C:somefile.dat", FileMode.Open)
    Dim bf As New BinaryFormatter()
    Dim stct As test = Ctype(bf.Deserialize(), test)
    fs.Close()
    [/code]

    If u need to store multiple structures of the same kind in a file then create an array or arraylist of structures and then pass the array or arraylist into the Binary Formatter.

  • ga0510ga0510 Posts: 1Member
    : I have a struct in vb.net like:
    :
    : structure test
    : dim element1 as string
    : dim element2 as integer
    : dim element3 as bool
    : end structure
    :
    : dim varTest as test
    :
    :
    : I want to write and read the whole struct variable varTest
    : in a file.
    :
    : I already try with fileput command like
    :
    : fileput(freefile,binarymodeRead,varTest)
    : and it looks like it work
    :
    : but when I try to read de struct with fileget
    : fileget(freefile,binarymodeWrite, varTest)
    : the app give me this error
    :
    : Is there another way to read/write the whole struct into or from
    : a file.
    :
    :
    :
    This is what I do.. I noticed that you tried fileput with the binarymode read.. When you open your file it's already designated as a binary file..

    structure test
    : dim element1 as string
    : dim element2 as integer
    : dim element3 as bool
    : end structure
    :
    : dim varTest as test

    FileOpen(1, file, OpenMode.Binary, OpenAccess.Write)
    fileput(1,varTest)
    fileclose(1)

    then to read it back again

    FileOpen(1, file, OpenMode.Binary, OpenAccess.Read)
    fileget(1,varTest)
    fileclose(1)

    This works for me just fine.
Sign In or Register to comment.