Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm an old VB hack, many years retired, recently spurred to write some code to make add-on utilities for a game.
The game file comprises a variable number of variable length chunks, two of which contain floating point arrays. Having written apps in VB6 to read and modify the files which work pretty well, I thought I'd update my skills and rewrite them in VB.net so that I wouldn't have to burden my (potential) users with having to install the common dialog and other OCX's.
I have spent three days ploughing my way through the obtuse, obscure and downright bewildering documentation of file handling in VB.net without learning how I can read/write string, byte and float data from the same file.
At present I use input to grab some string data, within which I get pointers to the float data. I then use SEEK and GET to read my float arrays and once again SEEK and then PUT to rewrite the amended data.
Can anyone offer advice, please?

What I have tried:

I have looked at Filestreams and My.computer.filesystem.FileGet but find my brain addled by needing to read different bits of the file in different ways.
Posted
Updated 17-Mar-19 5:21am
Comments
CPallini 17-Mar-19 7:12am    
Since you have to learn a new language, why don't you start directly with C# (I think you might get better help)?

Have a look at this: ByteArrayBuilder - a StringBuilder for Bytes[^] the code is in C#, but Code Converter C# to VB and VB to C# – Telerik[^] should help if you can't work it out.

Actually reading and writing a file full of bytes is simple: File.ReadAllBytes(String) Method (System.IO) | Microsoft Docs[^] and File.WriteAllBytes(String, Byte[]) Method (System.IO) | Microsoft Docs[^] will do it for you. Once you have it as an array of bytes, it's pretty easy to process into "native data".
 
Share this answer
 
Comments
Maciej Los 17-Mar-19 6:45am    
5ed!
Member 14100511 17-Mar-19 9:17am    
Thanks for the reply Original Griff. Maciej Los - I have no idea what 5ed! is meant to mean.
So maybe I didn't write my qyestion well. My problem is *not* how to write an array to a file, it's how do I read a string of characters, then reset file pointer and read bytes, then reset file pointer an read an array of floats, read some more bytes, reset the file pointer and read another float array. Once I have managed that I will want to reset the pointer and rewrite the two float arrays, and if I'm going to have to figure out how to convert C#, (which I am not conversant with) to VB.NET, (which I am not conversant with) I can forsee that I will never complete the project.
OriginalGriff 17-Mar-19 9:26am    
"5ed!" is related to the reputation system at CP: if you look above this you will find a line that says "Solution 1" - look to the right and you will see five stars. That's the "reputation" that this solution has got. In this case, 1 vote of "five stars" which means that Maciej Los thought it a good answer to your question as stated. 5 starts is an "up vote" which gives positive reputation to the author, 1 stars is a "down vote" which gives negative rep.
You can pretty much ignore it, or participate - it's up to you.

If you read the whole file into an array of bytes, you can access it as random access memory using the array index as an "addressing system". You can then convert the bytes to any datatype you want pretty easily - and the link I gave you has code which does exactly that: it works from a buffer, but that's pretty trivial to change. Have a look at some of the methods in teh download, and you will find code to convert a number of bytes to strings, characters, integers, shorts, doubles, ... pretty much all the "standard types" are covered.

That lot should give you enough to do what you want to without too many problems.
Member 14100511 17-Mar-19 9:48am    
Okay,
I'll leave the code as VB6. From what's been said it would seem that Microsoft in their infinite wisdom have stripped away the common sense from VB.

Instead of just doing the things I want, I have to do something else and then do a bunch of other stuff so I can get on and do the things I want to do, then do another bunch of stuff to effect my changes.
If this site has the equivalent of moderators, please delete my question and remove my membership.
Thank you.
OriginalGriff 17-Mar-19 10:02am    
No, they've moved it from a - fairly crappy - minority language to one that is usable across a number of environments (from Windows, to web, to cloud, to iPhone and Android) even if it is still considered a "baby steps" language by many when compared to C# or C++.

There is a lot to learn, yes - because the framework is huge, consistent, and contains a lot of stuff that really does make you life easier when you are used to thinking in a "Modern way". VB6 pretty much died fifteen years ago, and sticking to it is very much a retrograde step.

What I'd suggest is that you should forget VB for the moment, and learn C# and .NET instead (so that your experience with VB6 doesn't encourage you to use outdated constructs instead of the newer equivalents). The language itself shouldn't take more than a week (heck, an experienced developer can get enough to get by with in an afternoon!), but the .NET framework will take a good amount of time, there is a HUGE amount in there. Get a book - Wrox and Addison Wesley do some good ones, and Petzold is free:
http://www.charlespetzold.com/dotnet/index.html
Though C++ background based in the main.

Once you get used to it, it makes life a whole load easier, and development faster and more reliable.
If you must try to do a "line for line" conversion of the code (I DONT'T recommend it!), you're looking for the BinaryReader[^] and BinaryWriter[^] classes. It lets you do all kinds of reads and writes for various types (byte, int16, int32, int64, signed byte, single, ...)

They don't provide any functionality to Seek around the file though. That's easy enough to do by opening the file with a FileStream, which will allow you to seek around. This FileStream can then be passed to a BinaryReader/Writer which will allow you to read/write exactly what you want.
Using myFile As FileStream = File.Open(filename, FileMode.Open)
    Using fileContent As New BinaryReader(myFile)
        myFile.Seek(position)
        Dim someValue As Integer = fileContent.ReadInt32();
    End Using
End Using
 
Share this answer
 
I was thinking about your requirements to store and retrieve data of game...

I'd start from Serialization (Visual Basic) | Microsoft Docs[^], especially about BinaryFormatter Class (System.Runtime.Serialization.Formatters.Binary) | Microsoft Docs[^]. This enables you to write and read any data and avoids of types converting issues.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900