Sign in to follow this  

[.net] Problems with FileGet

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, i was wondering if anyone could help me out. I open a file for read/write access in my program. It has to be in binary mode, because i will never know the length of my data. I can easily write data to the file, and i can open it up into notepad and it saves it correctly, but for some reason(s), i cannot load it back into memory. <code> Dim filenumber as Integer Dim xmlbuff as String filenumber = FreeFile() FileOpen(filenumber, "store.xml", OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.LockReadWrite) FileGet(filenumber, xmlbuff) 'Then whenever i test to see what xmlbuff has, it comes up as empty or Null or "", depending on what kind of object i use 'So for simplicity I tried to use a string and it still did not work FilePut(filenumber, xmlbuff) 'Yet this always correctly writes the data to the file FileClose(filenumber) </code> Am I overlooking something? I even try to use the position and set it to 1, but that doesn't put anything in xmlbuff either :( I am starting to get frustrated because I don't know why it isn't working, I am following the examples ms provides :(

Share this post


Link to post
Share on other sites
I think the thing that's missing from your code is the initialization of the string. The string has to have a size to be able to act as a buffer.

So change Dim xmlbuff As String
To: Dim xmlbuff As New String(" ", 500)

See:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vblr7/html/vafctGet.htm

If you are reading xml I think that you should consider 'real' .NET functions and classes:
File, XmlDocument and streams

The functions you are using now are really a quick and dirty way of using old VB6 functions in .NET

Cheers

Share this post


Link to post
Share on other sites
hello, thanks for the little tip, i wasnt setting the size of the string. I will try that. Now does fileget or any other function have a way to store and entire file in a string? Lets say i wanted to open a proprietary structure, and it could be variable length, how could i go about doing that? Or would i just keep going through the file and concatenating a string with the buffer using filefet with a seek statement? I could use preexisting dot net features, but my project has to be very light weight, and alot of the xml features i just will not be using.

Or does it even matter, does it already add all the overhead even w/o using the features?

Share this post


Link to post
Share on other sites
Take a look at this code:

Dim doc as New XmlDocument()
Dim stream As Stream
stream = File.Open("mydata.xml")
doc.LoadXml(stream)
stream.Close()


It doesn't matter how big the xml file is. You don't have to worry about allocating enough space. And best of all: you get to use functions like:

Dim node As XmlNode
node = doc.SelectSingleNode("/myroot/subelement")
Dim interestingInfo as String
interestingInfo = node.Attributes("myattribute").Value


Using the functions that you do is really using wrappers arounf these classes and functions!

Cheers

Share this post


Link to post
Share on other sites

This topic is 4861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this