READFILE$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 4: Line 4:


{{PageSyntax}}
{{PageSyntax}}
: {{Parameter|content$}} = [[_READFILE$]]({{Parameter|fileSpec$}})
: {{Parameter|contents$}} = [[_READFILE$]]({{Parameter|fileSpec$}})




{{PageParameters}}
{{PageParameters}}
* {{Parameter|content$}} is the entire file contents returned as [[STRING]]. Maybe empty, if the file specified was an empty file, or if the program is continued from an file related [[ERROR Codes|ERROR]].
* {{Parameter|contents$}} is the entire file contents returned as [[STRING]]. May return an empty string, if the specified file was empty, or if the program was continued from a file related [[ERROR Codes|ERROR]].
* {{Parameter|fileSpec$}} is the name of the file to read as literal or variable [[STRING]], if required inclusive a full or relative path.
* {{Parameter|fileSpec$}} is the name of the file to read as literal or variable [[STRING]], if required inclusive a full or relative path.
** To avoid errors you should use [[_FILEEXISTS]] before calling this function to make sure the file exists.




{{PageDescription}}
{{PageDescription}}
* Sometimes it's required or at least useful to have the whole contents of a file in a single string for further processing, e.g. to pass it to hasing or compression functions which expect strings.
* Sometimes it's required or at least useful to have the whole contents of a file in a single string for further processing, e.g. to pass it to hashing or compression functions which expect strings.
* In earlier versions of QB64(PE) you had to implement that loading process manually all the time or create a reusable custom [[FUNCTION]] for it.
* In earlier versions of QB64(PE) you had to implement that loading process manually all the time or create a reusable custom [[FUNCTION]] for it.
* Now '''_READFILE''' will simplify this, it's mainly a convenience function to wrap the following code sequence in one handy function:
* Now '''_READFILE$''' will simplify this, it's mainly a convenience function to wrap the following code sequence into one handy function:
{{TextStart}}
{{TextStart}}
fh = {{Cb|FREEFILE}}
fh = {{Cb|FREEFILE}}
{{Cb|OPEN}} fileSpec$ {{Cb|FOR}} {{Cb|BINARY}} {{Cb|AS}} #fh
{{Cb|OPEN}} fileSpec$ {{Cb|FOR}} {{Cb|BINARY}} {{Cb|AS}} #fh
content$ = {{Cb|SPACE$}}({{Cb|LOF}}(fh))
contents$ = {{Cb|SPACE$}}({{Cb|LOF}}(fh))
{{Cb|GET}} #fh, , content$
{{Cb|GET}} #fh, , contents$
{{Cb|CLOSE}} #fh
{{Cb|CLOSE}} #fh
{{TextEnd}}
{{TextEnd}}
Line 60: Line 61:
{{Cl|COLOR}} White
{{Cl|COLOR}} White
{{Cl|PRINT}} {{Cl|_READFILE$}}(fileSpec$)
{{Cl|PRINT}} {{Cl|_READFILE$}}(fileSpec$)
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{Small|Example by RhoSigma}}


----
----


;Example 2: Passing a whoie file's contents to hasing functions.
;Example 2: Passing a whole file's contents to hashing functions.
{{CodeStart}}
{{CodeStart}}
{{Cm|$COLOR}}:{{Text|0|#F580B1}}
{{Cm|$COLOR}}:{{Text|0|#F580B1}}
Line 80: Line 84:
{{Cl|COLOR}} White
{{Cl|COLOR}} White
{{Cl|PRINT}} {{Cl|_MD5$}}({{Cl|_READFILE$}}(fileSpec$))
{{Cl|PRINT}} {{Cl|_MD5$}}({{Cl|_READFILE$}}(fileSpec$))
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}
{{Small|Example by RhoSigma}}




{{PageSeeAlso}}
{{PageSeeAlso}}
* [https://qb64phoenix.com/forum/showthread.php?tid=2699 Featured in our "Keyword of the Day" series]
* [[_WRITEFILE]], [[BLOAD]], [[BSAVE]]
* [[_DEFLATE$]], [[_INFLATE$]]
* [[_DEFLATE$]], [[_INFLATE$]]
* [[_ADLER32]], [[_MD5$]]
* [[_ADLER32]], [[_CRC32]], [[_MD5$]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 22:42, 25 May 2024

The _READFILE$ function returns the complete contents of a file in a single string, but without the usual overhead. It does OPEN, GET and CLOSE the file in one run.


Syntax

contents$ = _READFILE$(fileSpec$)


Parameters

  • contents$ is the entire file contents returned as STRING. May return an empty string, if the specified file was empty, or if the program was continued from a file related ERROR.
  • fileSpec$ is the name of the file to read as literal or variable STRING, if required inclusive a full or relative path.
    • To avoid errors you should use _FILEEXISTS before calling this function to make sure the file exists.


Description

  • Sometimes it's required or at least useful to have the whole contents of a file in a single string for further processing, e.g. to pass it to hashing or compression functions which expect strings.
  • In earlier versions of QB64(PE) you had to implement that loading process manually all the time or create a reusable custom FUNCTION for it.
  • Now _READFILE$ will simplify this, it's mainly a convenience function to wrap the following code sequence into one handy function:
fh = FREEFILE
OPEN fileSpec$ FOR BINARY AS #fh
contents$ = SPACE$(LOF(fh))
GET #fh, , contents$
CLOSE #fh


Availability


Examples

Example 1
Showing that this function's result is equal to the code sequence shown above.
$COLOR:0

fileSpec$ = "source\global\settings.bas"

fh = FREEFILE
OPEN fileSpec$ FOR BINARY AS #fh
content$ = SPACE$(LOF(fh))
GET #fh, , content$
CLOSE #fh

COLOR LightGreen
PRINT "Using old manual load method..."
COLOR White
PRINT content$

COLOR LightGreen
PRINT "Using new direct load method..."
COLOR White
PRINT _READFILE$(fileSpec$)

END
Example by RhoSigma

Example 2
Passing a whole file's contents to hashing functions.
$COLOR:0

fileSpec$ = "source\global\settings.bas"

COLOR LightGreen
PRINT "CRC32 of the file..."
COLOR White
PRINT RIGHT$("00000000" + HEX$(_CRC32(_READFILE$(fileSpec$))), 8)
PRINT

COLOR LightGreen
PRINT "MD5 of the file..."
COLOR White
PRINT _MD5$(_READFILE$(fileSpec$))

END
Example by RhoSigma


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link