READFILE$: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
(Created page with "{{DISPLAYTITLE: _READFILE$}} 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. {{PageSyntax}} : {{Parameter|content$}} = _READFILE$({{Parameter|fileSpec$}}) {{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 contin...") |
No edit summary |
||
Line 13: | Line 13: | ||
{{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 | * 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. | ||
* 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 in one handy function: | ||
Line 39: | Line 39: | ||
{{PageExamples}} | {{PageExamples}} | ||
;Example: Showing | ;Example 1: Showing that this function's result is equal to the code sequence shown above. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cm|$COLOR}}:{{Text|0|#F580B1}} | |||
fileSpec$ = {{Text|<nowiki>"source\global\settings.bas"</nowiki>|#FFB100}} | |||
fh = {{Cl|FREEFILE}} | |||
{{Cl|OPEN}} fileSpec$ {{Cl|OPEN#File_Access_Modes|FOR}} {{Cl|OPEN#File_Access_Modes|BINARY}} {{Cl|OPEN|AS}} #fh | |||
content$ = {{Cl|SPACE$}}({{Cl|LOF}}(fh)) | |||
{{Cl|GET}} #fh, , content$ | |||
{{Cl|CLOSE}} #fh | |||
{{Cl|COLOR}} LightGreen | |||
{{Cl|PRINT}} {{Text|<nowiki>"Using old manual load method..."</nowiki>|#FFB100}} | |||
{{Cl|COLOR}} White | |||
{{Cl|PRINT}} content$ | |||
{{Cl|COLOR}} LightGreen | |||
{{Cl|PRINT}} {{Text|<nowiki>"Using new direct load method..."</nowiki>|#FFB100}} | |||
{{Cl|COLOR}} White | |||
{{Cl|PRINT}} {{Cl|_READFILE$}}(fileSpec$) | |||
{{CodeEnd}} | |||
---- | |||
;Example 2: Passing a whoie file's contents to hasing functions. | |||
{{CodeStart}} | |||
{{Cm|$COLOR}}:{{Text|0|#F580B1}} | |||
fileSpec$ = {{Text|<nowiki>"source\global\settings.bas"</nowiki>|#FFB100}} | |||
{{Cl|COLOR}} LightGreen | |||
{{Cl|PRINT}} {{Text|<nowiki>"CRC32 of the file..."</nowiki>|#FFB100}} | |||
{{Cl|COLOR}} White | |||
{{Cl|PRINT}} {{Cl|RIGHT$}}({{Text|<nowiki>"00000000"</nowiki>|#FFB100}} + {{Cl|HEX$}}({{Cl|_CRC32}}({{Cl|_READFILE$}}(fileSpec$))), {{Text|8|#F580B1}}) | |||
{{Cl|PRINT}} | |||
{{Cl|COLOR}} LightGreen | |||
{{Cl|PRINT}} {{Text|<nowiki>"MD5 of the file..."</nowiki>|#FFB100}} | |||
{{Cl|COLOR}} White | |||
{{Cl|PRINT}} {{Cl|_MD5$}}({{Cl|_READFILE$}}(fileSpec$)) | |||
{{CodeEnd}} | {{CodeEnd}} | ||
Revision as of 20:34, 13 February 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
- content$ = _READFILE$(fileSpec$)
Parameters
- 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.
- fileSpec$ is the name of the file to read as literal or variable STRING, if required inclusive a full or relative path.
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 hasing 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 in one handy function:
fh = FREEFILE OPEN fileSpec$ FOR BINARY AS #fh content$ = SPACE$(LOF(fh)) GET #fh, , content$ 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$) |
- Example 2
- Passing a whoie file's contents to hasing 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$)) |
See also