Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A Question on memory set aside for Zero
#10
@Dimster Kindly read over the code below to get a quick understanding of it, and then run it as well.  The comments inside the code are made to go with the output that the program generates, and I hope they'll help clear up any issues you have about what's going on here, and how you probably need to proceed to do what you're wanting to do.

Code: (Select All)
Screen _NewImage(800, 600, 32)
_ControlChr Off
'Let's go with some DATA to work with, to illustrate what's going on.
example_data:
Data 1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0,11,12,13,14,15,123,1234,12345,123456,1234567,12345678,123456789,123457890,-1

'and let's make a SINGLE variable to hold that data
Dim MyData As Single

'Now, let's open a file a WRITE that data to file.
Open "temp.txt" For Output As #1
Restore example_data
Do
    Read MyData
    Write #1, MyData
Loop Until MyData = -1
Close

Print "Data written to 'temp.txt'.  Please take a moment and look at that data file and see how it looks."
Print "It should look like the following:"
Open "temp.txt" For Input As #1
For i = 1 To 30
    Input #1, MyData
    Print MyData
Next
Close

Print
Print "Press <ANY KEY> to continue."
Sleep

Cls
Print "Now, as you can see, that data WAS stored in a SINGLE, which is only 4-bytes in size,"
Print "but once we WRITE it to file, it gets printed in a human-readable format, which"
Print "varies in size due to the length of the data.  One can't simply SEEK to any record in"
Print "this file that they want to.  It needs to be read sequentially from start to finish."
Print
Print "IF one wants a RANDOM file so that they can SEEK to any of these particular"
Print "records instantly, then they must make the file without using WRITE and OUTPUT."
Print "Instead, they *must* use RANDOM (or BINARY) and GET/PUT."
Print
Print "Press <ANY KEY> to continue."
Sleep

'let me illustrate below for you:
'we'll use the same data as before, but this time I'm going to write it to a differnt file to compare.
Open "temp_RANDOM.txt" For Random As #1 Len = 4 'only one field, so my records only need to be 4-byte fields
Restore example_data
Do
    Read MyData
    Put #1, , MyData
Loop Until MyData = -1
Close

Cls
Print "Data written to 'temp_RANDOM.txt'.  Please take a moment and look at that data file and see how it looks."
Print "It should look like the following:"
Open "temp_RANDOM.txt" For Input As #1
Line Input #1, file_data$
Print file_data$
Close
Print
Print "Notice how that's not at all human-readable?  That's written in the same 4-byte"
Print "Pattern as a SINGLE is stored in memory."
Print "That data can be SEEKED to, as we know the size -- 4 bytes per record"
Print "OR we can use RANDOM to access it, as we do below for records #3, 7, and 17."
Open "temp_RANDOM.txt" For Random As #1 Len = 4 'only one field, so my records only need to be 4-byte fields
Get #1, 3, MyData: Print MyData
Get #1, 7, MyData: Print MyData
Get #1, 17, MyData: Print MyData
Close
Print
Print "Press <ANY KEY> to continue."
Sleep

Cls
Print "As you can tell by comparing the two data files and their structures,"
Print "the file created with WRITE is nothing like the file created with RANDOM."
Print "It would be more-or-less impossible to use SEEK with the file written with WRITE,"
Print "to just jump back and forth between data fields."
Print
Print "IF one wishes to use SEEK or RANDOM, then the file would need to be created in that manner"
Print "to begin with.  Files written FOR OUTPUT and using WRITE simply don't fit the proper format."
Print "Convert them with a quick and simply utility like the one included below."
Print
'Now, let's convert the first file to suit the second file's formatting style.
Open "temp.txt" For Input As #1 'This is our original file.
Open "temp_CONVERTED" For Random As #2 Len = 4 'the file we're going to convert to.
For i = 1 To 30
    Input #1, MyData 'read the original file which was written FOR OUTPUT
    Put #2, , MyData 'write it in the converted file FOR RANDOM access
Next
Close

'and now, to show off the data from that converted file, let's get the same three records as before
'and print them to the screen.  This is record #3, 7, and 17 in the converted file.
Open "temp_Converted" For Random As #1 Len = 4
Get #1, 3, MyData: Print MyData
Get #1, 7, MyData: Print MyData
Get #1, 17, MyData: Print MyData
Close
Print
Print "See the differences in working with a file written FOR OUTPUT with WRITE,"
Print "and a file written FOR RANDOM, with PUT?"


What you have, is a file opened FOR OUTPUT and created using WRITE to write the data to the file.  SEEK really isn't going to work for you, as you're going to have variable-length data by using WRITE to store the information on the file.

Converting a file of simple values like this from one file type (WRITE format) to another (RANDOM format) is a breeze, and it seems like that's what you really should do here.  Convert it over to the format you want and then use the converted file.  It's the simplest way to do what you're trying to do here, I think.
Reply


Messages In This Thread
RE: A Question on memory set aside for Zero - by SMcNeill - 12-01-2024, 02:53 PM



Users browsing this thread: 11 Guest(s)