_memput/get from file. - ahenry3068 - 09-19-2025
There are several times in recent projects where it would have been useful to do a File GET directly to a _MEM buffer.
Not sure what the best syntax for such a command would be but it should be possible I think. !..
It would also be able to do the inverse and PUT a _MEM buffer directly to a file as well !
RE: _memput/get from file. - SMcNeill - 09-19-2025
Are new keywords really needed? Something like this is so simple, just write a quick set of functions, add it to your toolbox and be done with it.
Code: (Select All)
Dim Astring As String * 20
Open "temp.txt" For Output As #1 'create a file
For i = 1 To 20
Astring = "Hello World # " + _ToStr$(i)
Print #1, Astring;
Next
Close
Print "File created, now to test -- press any key:"
Sleep
'Now for some quick testing:
Dim foo As _MEM
MemOpenfile "temp.txt", foo 'get the file into mem
For i = 0 To 19
_MemGet foo, foo.OFFSET + i * 20, Astring
Print Astring
Next
For i = 0 To 19 'rewrite the data and put to mem
Astring = "Steve is Amazing " + _ToStr$(i + 1)
_MemPut foo, foo.OFFSET + 20 * i, Astring
Next
MemSaveFile foo, "temp2.txt" 'save the mem into to a file
Print "File loaded, displayed, changed, and saved as temp2.txt."
Print "Press "
Sleep
Open "temp2.txt" For Binary As #1
For i = 1 To 20
Get #1, , Astring
Print Astring
Next
Close
Sub MemOpenfile (file$, m As _MEM)
Dim temp As String
temp$ = _ReadFile$(file$)
m = _MemNew(Len(temp$))
_MemPut m, m.OFFSET, temp$
End Sub
Sub MemSaveFile (m As _MEM, file$)
Dim temp As String
temp = Space$(m.SIZE)
_MemGet m, m.OFFSET, temp
_WriteFile file$, temp
End Sub
RE: _memput/get from file. - ahenry3068 - 09-19-2025
(09-19-2025, 08:10 PM)SMcNeill Wrote: Are new keywords really needed? Something like this is so simple, just write a quick set of functions, add it to your toolbox and be done with it.
Code: (Select All)
Dim Astring As String * 20
Open "temp.txt" For Output As #1 'create a file
For i = 1 To 20
Astring = "Hello World # " + _ToStr$(i)
Print #1, Astring;
Next
Close
Print "File created, now to test -- press any key:"
Sleep
'Now for some quick testing:
Dim foo As _MEM
MemOpenfile "temp.txt", foo 'get the file into mem
For i = 0 To 19
_MemGet foo, foo.OFFSET + i * 20, Astring
Print Astring
Next
For i = 0 To 19 'rewrite the data and put to mem
Astring = "Steve is Amazing " + _ToStr$(i + 1)
_MemPut foo, foo.OFFSET + 20 * i, Astring
Next
MemSaveFile foo, "temp2.txt" 'save the mem into to a file
Print "File loaded, displayed, changed, and saved as temp2.txt."
Print "Press "
Sleep
Open "temp2.txt" For Binary As #1
For i = 1 To 20
Get #1, , Astring
Print Astring
Next
Close
Sub MemOpenfile (file$, m As _MEM)
Dim temp As String
temp$ = _ReadFile$(file$)
m = _MemNew(Len(temp$))
_MemPut m, m.OFFSET, temp$
End Sub
Sub MemSaveFile (m As _MEM, file$)
Dim temp As String
temp = Space$(m.SIZE)
_MemGet m, m.OFFSET, temp
_WriteFile file$, temp
End Sub
That's easy enough. But my thought was also that a builtin function might also be a bit closer to the metal and thus faster. It's not really a big deal to me TBH.
But I do get ideas I need to throw out there from time to time !.
|