Page 1 of 1

Does "IndudeBinary" works with Variable?

Posted: Sun Dec 10, 2023 5:55 pm
by Maya
Good evening all,
Is it possible to pass a Variable to "IndudeBinary"?
I get the following error when writing below code.
"Only a literal string can be put after IndudeBinary".

Code: Select all

Procedure SayHi(File.s)
  DataSection
    ImportDataStart:
    IncludeBinary File
    ImportDataEnd:
  EndDataSection
EndProcedure

SayHi("D:\test.txt")

Re: Does "IndudeBinary" works with Variable?

Posted: Sun Dec 10, 2023 5:59 pm
by Fred
IncludeBinary is executed at compile time, so you can use a variable for it. That said you can use a Macro instead of the procedure and it should work.

Re: Does "IndudeBinary" works with Variable?

Posted: Sun Dec 10, 2023 6:05 pm
by normeus
Include happens at compile time. You can not choose to change the contents of your program after it has been compiled.

Code: Select all

IncludeBinary will include the named file at the current place in the code. Including should be done inside a Data block. 


You should use readdata instead, Lof() Example:

Code: Select all

  file$ = OpenFileRequester("Select a file","","Text (.txt)|*.txt|All files (*.*)|*.*",0)
  If file$
    If ReadFile(0, file$)
      length = Lof(0)                            ; get the length of opened file
      *MemoryID = AllocateMemory(length)         ; allocate the needed memory
      If *MemoryID
        bytes = ReadData(0, *MemoryID, length)   ; read all data into the memory block
        Debug "Number of bytes read: " + Str(bytes)
      EndIf
      CloseFile(0)
    EndIf
  EndIf


Norm

Re: Does "IndudeBinary" works with Variable?

Posted: Sun Dec 10, 2023 6:49 pm
by Maya
Fred wrote: Sun Dec 10, 2023 5:59 pm IncludeBinary is executed at compile time, so you can use a variable for it. That said you can use a Macro instead of the procedure and it should work.
Hi Fred!
Yes, it worked in a Macro, however it doesn't work in the following case:

Code: Select all

Macro SayHi(File)
  DataSection
    ImportDataStart:
    IncludeBinary File
    ImportDataEnd:
  EndDataSection
EndMacro

Var.s = "D:\test.txt"     ; <<< doesn't work.
SayHi(Var)

Re: Does "IndudeBinary" works with Variable?

Posted: Sun Dec 10, 2023 7:04 pm
by skywalk
BEFORE your code compiles, the Macro's are expanded to their actual code representation.
Variables are undefined or empty, so Include's cannot act correctly.
If your Macro uses a literal string, then it will be seen when Include's are executed.

DataSection's are also available before compile.
That is why it was suggested to put your data or filenames there.