Link to home
Start Free TrialLog in
Avatar of gerrymcd
gerrymcdFlag for Ireland

asked on

creating a data file in vb, for a vb app

how do i create a file to hold sounds, pictures for a vb app, and load it in that app.

im writing a multiple choice questions prog and i want to create a data file that can contain sound files/pictue files is this possible? /how do i do it?
Avatar of aut
aut

Create a resource file.
Try out the HPI format used by the game Total Anilation. There's a VB 5 demo on using it at http://www.cws.org/~joed/ta/hpiutil.zip.
Avatar of gerrymcd

ASKER

tsturn!
i want to know how to do it with vb
deat qut,
how do i create a resource file?
ASKER CERTIFIED SOLUTION
Avatar of aut
aut

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can store multiple files inside one file using simple Put statements. Something like this..

Dim ByteArray() As Byte

Open destfilename For Binary As #1

Open file1 For Binary As #2
ReDim ByteArray(LOF(2) - 1)
Get #2, , ByteArray
Put #1, , LOF(2)
Put #1, , ByteArray
Close #2

..
..

Open file9999... For Binary As #2
ReDim ByteArray(LOF(2) - 1)
Get #2, , ByteArray
Put #1, , LOF(2)
Put #1, , ByteArray
Close #2

Close #1


If you do this you can read back the info using Get statements like this (this example reads back the first file that was saved)..

Dim ByteArray() As Byte
Dim lValue As Long        <- to store filesize

Open destfilename For Binary As #1
Get #1, , lValue
ReDim ByteArray(lValue - 1)
Get #1, , ByteArray
Close #1


You will have to store the byte array to disk before you can use it, unless you use some 3rd party control that can handle byte arrays (= $$$). You store the byte array to a file with code like

Open filetemp For Binary As #1
Put #1, , ByteArray
Close #1


Then you can use the filetemp in your program, for example if it's a picture you can use code like "Picture1.Picture = LoadPicture(filetemp)" to show it in Picture1. When you're done with it just remove it from your disk.

Using this scheme will make your big file look something like this..

***********************************************
* how big is the next file? (Long value) *
* Byte Array containing information      *
**********************************************
..
..
***********************************************
* how big is the next file? (Long value) *
* Byte Array containing information      *
**********************************************
vbmaster: what you say is also true.

BUT

(a) The images/sounds will not be included within the EXE file. With resource files, everything is within the EXE, and they are loaded into memory as needed.

(b) If the programmer needs to modify a single file from the component files, the following has to be done:
    - Modify the file
    - Regenerate the large single file
    - Redefine the size of each file in the program
    - Recompile the EXE
Using the resource compiler, it become a lot easier.

(c) Reading the files cannot be done with built-in VB functions. The programmer will have to create his own functions to access the "big file"

(d) This will also mean creating code for writing, reading, and deleting temporary files

As you see, most of the above is unnecessary, if the programmer had initially used resource files...
Well, I would NOT recommend to do exactly what I wrote, but I drew up the ideas how to work. Of course you will need to do some works, I would probably add all the filesizes to the end of the big file instead of the start of every small file, this way all the filesizes can be read at the same time and if you know the filesizes you also know exactly where to start in the big file when you want to 'extract' a file.

Resource files are very good to work with... BUT if you read gerrymcd's question it sure sounds like he wants these big files outside the exe's..

"I'm writing a multiple choice questions prog and i want to create a data file that can contain sound files/pictue files is this possible?"

Let's say he uses resource files, then he would be stuck with the number of questions he made when he compiled the project, if he wants to add questions he would have to recompile the whole project.

And bigger executables means longer time to start and probably more memory is used. I do not know if it's true but I also have experienced that bigger executables also means a lot worse performance than using a small executable.

"(c) Reading the files cannot be done with built-in VB functions. The programmer will have to create his own functions to access the "big file""

Is everybody here afraid of acutal 'coding'? ;)
vbmaster:

"Is everybody here afraid of acutal 'coding'?"
;-))
Actually no, but I meant it from a performance standpoint...

As far as I know, resources from resource files are only loaded (and hence take up memory) when they are needed.

gerrymcd,
if you want to include a fixed set of files, go for a resource file. If you think you will be changing these files often, then go for vbmaster's idea, it's more expandable -- IMHO.