Does "IndudeBinary" works with Variable?

Just starting out? Need help? Post your questions and find answers here.
Maya

Does "IndudeBinary" works with Variable?

Post 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")
Fred
Administrator
Administrator
Posts: 18352
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Does "IndudeBinary" works with Variable?

Post 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.
normeus
Enthusiast
Enthusiast
Posts: 475
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Does "IndudeBinary" works with Variable?

Post 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
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Maya

Re: Does "IndudeBinary" works with Variable?

Post 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)
User avatar
skywalk
Addict
Addict
Posts: 4242
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Does "IndudeBinary" works with Variable?

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply