• Welcome to Jose's Read Only Forum 2023.
 

OPEN FOR INPUT in PureBASIC

Started by Israel Vega, May 12, 2015, 04:31:30 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Israel Vega

This Function's or PROCEDURE is for "emulate" the OPEN FOR INPUT from a traditional BASIC.  Not equal but similar syntax:


Procedure.L OPENFORINPUT(NAMEFILE.S)
  RESULT=ReadFile(#PB_Any,NAMEFILE)
  ProcedureReturn RESULT
EndProcedure

Procedure.S LINEINPUT(BUFFER)
  LINEA$=ReadString(BUFFER)
  ProcedureReturn LINEA$
EndProcedure


;Probar OPEN FOR INPUT
BUFFER=OPENFORINPUT("c:\texto.txt")

Repeat
  VALOR$=LINEINPUT(BUFFER)
  MessageRequester(Str(BUFFER),VALOR$,0) 
Until Eof(BUFFER)<>0



will continue emulating the OPEN FOR OUTPUT,   OPEN FOR APPEND,  OPEN FOR RANDOM and OPEN FOR BINARY

Israel Vega

#1
Whit this sample is possible use of open files for INPUT or open for RANDOM in PureBasic:


Structure Employees
  Code.s{10}
  Name.s{20}
EndStructure

ESTRUCTURA = Employees

Procedure.L OpenForInput(BUFFER,NAMEFILE.S)
  RESULT=ReadFile(BUFFER,NAMEFILE)
  ProcedureReturn RESULT
EndProcedure

Procedure.S LineInput(BUFFER)
  LINEA$=ReadString(BUFFER)
  ProcedureReturn LINEA$
EndProcedure


Procedure.L OpenForRandom(NAMEFILE.S,BUFFER.L,LONGITUD.L,FLAGS.L)
  RESULT=OpenFile(BUFFER, NAMEFILE,FLAGS)
  ProcedureReturn RESULT
EndProcedure


Procedure GET(BUFFER, record_number,Employees)
  record_number=record_number-1
  *result.Employees = AllocateMemory(SizeOf(Employees))
  FileSeek(BUFFER, record_number * SizeOf(Employees))
  If ReadData(BUFFER, *result, SizeOf(Employees)) = SizeOf(Employees)
    ProcedureReturn *result
  Else
    ProcedureReturn 0
  EndIf
EndProcedure

Procedure PUT(BUFFER, record_number, *record.Employees)
  record_number=record_number-1
  FileSeek(BUFFER, record_number * SizeOf(Employees))
  If WriteData(BUFFER, @*record\code, SizeOf(Employees\code)) = SizeOf(Employees\code) And
     WriteData(BUFFER, @*record\name, SizeOf(Employees\name)) = SizeOf(Employees\name)
     ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
EndProcedure



;Probar OpenForInput
BUFFER=1
OpenForInput(BUFFER,"texto.txt")

While Eof(BUFFER)=#False
  VALOR$=LineInput(BUFFER)
  MessageRequester(Str(BUFFER)+"XXX",VALOR$,0) 
Wend



;Probar OpenForRandom
BUFFER=1
FILENAME.S="DATOS.DAT"
OpenForRandom(FILENAME,BUFFER,SizeOf(Employees),0)

With employee.EMPLOYEES
\Code="1234"
\Name="ISRAEL VEGA ALVAREZ"
EndWith

PUT(BUFFER, 1, @employee)

With employee.EMPLOYEES
  \Code = "10000"
  \name = "Juan Perez"
EndWith

PUT(BUFFER, 2, @employee)

For X=3 To 100
 
  With employee.EMPLOYEES
   \Code="CODE"+Trim(Str(X))
   \Name="ISRAEL"+Trim(Str(X))
  EndWith

PUT(BUFFER, X, @employee)
 
Next X


*record.EMPLOYEES=GET(BUFFER,100,@employee)
FreeMemory(*record)
MessageRequester("",*record\name,0)




May be for many user not need but for me I use many files and I like versatile of traditional BASIC.