Zooker,
In the following code is a copy of the QB help example for the "PUT" function. I have included a substitute with procedures to somewhat emulate the behavior of the QB functions. I have used this sort of file and record I/O before to access data files created by legacy applications. As pdwyer mentioned, I would move to using a SQLite DB. It is miles ahead of the way QB used to do this, and is scalable to very large record numbers. As you are recoding anyway, it really makes sense. If you want to play though to get familiar with QB, I hope the attached code helps.
Code: Select all
;---------- Code from QB "PUT" help example ---------
; ' Read a name and a test score from the console.
; ' Store each name and score as a record in a
; ' random-access file.
;
; ' Define record fields.
; TYPE TestRecord
; NameField As STRING * 20
; ScoreField As SINGLE
; End TYPE
;
; ' Open the test data file.
; Dim FileBuffer As TestRecord
; OPEN "TESTDAT.DAT" For RANDOM As #1 LEN = Len(FileBuffer)
;
; ' Read pairs of names and scores from the console.
;
; CLS ' Clear screen
; I = 0
; DO
; I = I + 1
; INPUT "Name ? ", FileBuffer.NameField
; INPUT "Score? ", FileBuffer.ScoreField
; INPUT "-->More (y/n)? ", Resp$
; PUT #1, I, FileBuffer
; LOOP Until UCASE$(MID$(Resp$, 1, 1)) = "N"
;
; PRINT I; " records written."
;
; CLOSE #1
;------------- start of PB code -----------------
;setup structure
Structure TestRecord
NameField.s{20}
ScoreField.f
EndStructure
; Open the test data file.
Dim FileBuffer.TestRecord(0)
OpenFile(1,"C:\Program Files\PureBasic\Projects\Test\TESTDAT.DAT")
Global TestRecLen.w=SizeOf(TestRecord)
; Procedures to make this as QB as possible
Procedure QBPut(Filenum.w,RecordNum.w,*recordpointer)
FileSeek(FileNum,(RecordNum-1)*TestRecLen)
WriteData(Filenum,*recordpointer,TestRecLen)
EndProcedure
Procedure.s QBInput(InputString.s)
Print(Inputstring)
ProcedureReturn Input()
EndProcedure
;Open a console and clear it
OpenConsole()
EnableGraphicalConsole(1)
ClearConsole()
I=0
Repeat
I=I+1
FileBuffer(0)\NameField=QBInput("Name ? ")
FileBuffer(0)\Scorefield=ValF(QBInput("Score ? "))
Resp$=QBInput("-->More (y/n)? ")
QBPut(1,I,@FileBuffer(0))
Until UCase(Resp$)="N"
PrintN(Str(I) + " records written")
CloseFile(1)
;this will keep the console open until you hit a key
Repeat
Delay(100)
Until Inkey()<>""
The absolute beauty of PB is that a whole lot can be accomplished without a great deal of extra coding. You shoud be aware though that in PB strings are null terminated. In QB you could store chr$(00) in a string, in PB that will terminate the string and there will be no data after that. You will need to use memory buffers to store binary data. This make PB far more efficient as string handling tends to be slow in most languages. Directly manipulating memory is far more efficient.
Post back if you have any questions.
Edited
moved procs to after "Global TestRecLen" as otherwise the procs didn 't see the value.