Author Topic: OPEN FOR INPUT in PureBASIC  (Read 2632 times)

0 Members and 1 Guest are viewing this topic.

Offline Israel Vega

  • Newbie
  • *
  • Posts: 22
  • User-Rate: +0/-0
OPEN FOR INPUT in PureBASIC
« on: May 12, 2015, 04:31:30 AM »
This Function's or PROCEDURE is for "emulate" the OPEN FOR INPUT from a traditional BASIC.  Not equal but similar syntax:

Code: [Select]
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
« Last Edit: May 12, 2015, 04:33:51 AM by Israel Vega »

Offline Israel Vega

  • Newbie
  • *
  • Posts: 22
  • User-Rate: +0/-0
Re: OpenForInput and OpenForRandom in PureBASIC
« Reply #1 on: May 12, 2015, 10:15:27 PM »
Whit this sample is possible use of open files for INPUT or open for RANDOM in PureBasic:

Code: [Select]
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.
« Last Edit: May 12, 2015, 10:40:20 PM by Israel Vega »