WRITEFILE: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "{{DISPLAYTITLE: _WRITEFILE}} The '''_WRITEFILE''' function writes a string into a new file, overwriting an existing file of the same name. It does OPEN, PUT and CLOSE the file in one run. It's the counterpart to _READFILE$. {{PageSyntax}} : {{Parameter|success%}} = _WRITEFILE({{Parameter|fileSpec$}}, {{Parameter|contents$}}) {{PageParameters}} * {{Parameter|success%}} is an INTEGER return value, it shows whether the write operation was success...")
 
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE: _WRITEFILE}}
{{DISPLAYTITLE: _WRITEFILE}}
The '''_WRITEFILE''' function writes a string into a new file, overwriting an existing file of the same name. It does [[OPEN]], [[PUT]] and [[CLOSE]] the file in one run. It's the counterpart to [[_READFILE$]].
The '''_WRITEFILE''' statement writes a string into a new file, overwriting an existing file of the same name. It does [[OPEN]], [[PUT]] and [[CLOSE]] the file in one run. It's the counterpart to the [[_READFILE$]] function.




{{PageSyntax}}
{{PageSyntax}}
: {{Parameter|success%}} = [[_WRITEFILE]]({{Parameter|fileSpec$}}, {{Parameter|contents$}})
: [[_WRITEFILE]] {{Parameter|fileSpec$}}, {{Parameter|contents$}}




{{PageParameters}}
{{PageParameters}}
* {{Parameter|success%}} is an [[INTEGER]] return value, it shows whether the write operation was successful (-1) or not (0). Any file related [[ERROR Codes|ERROR]] may occur.
** To avoid errors you should use [[_PATHEXISTS]] before calling this function to make sure a used path exists.
* {{Parameter|fileSpec$}} is the name of the file to write as literal or variable [[STRING]], if required inclusive a full or relative path.
* {{Parameter|fileSpec$}} is the name of the file to write as literal or variable [[STRING]], if required inclusive a full or relative path.
** To avoid errors you should use [[_DIREXISTS]] before using this statement to make sure a desired path exists.
* {{Parameter|contents$}} is the literal or variable [[STRING]] which its contents shall be written into the file.
* {{Parameter|contents$}} is the literal or variable [[STRING]] which its contents shall be written into the file.


Line 17: Line 16:
* Sometimes you may be in need to quickly dump a huge amount of data into a file without much fuss, e.g. the results of the pack/unpack functions [[_DEFLATE$]] and [[_INFLATE$]] or when copying a file in conjunction with the [[_READFILE$]] function.
* Sometimes you may be in need to quickly dump a huge amount of data into a file without much fuss, e.g. the results of the pack/unpack functions [[_DEFLATE$]] and [[_INFLATE$]] or when copying a file in conjunction with the [[_READFILE$]] function.
* In earlier versions of QB64(PE) you had to implement that saving process manually all the time or create a reusable custom [[FUNCTION]] for it.
* In earlier versions of QB64(PE) you had to implement that saving process manually all the time or create a reusable custom [[FUNCTION]] for it.
* Now '''_WRITEFILE''' will simplify this, it's mainly a convenience function to wrap the following code sequence in one handy function:
* Now '''_WRITEFILE''' will simplify this, it's mainly for convenience to wrap the following code sequence into one handy statement:
{{TextStart}}
{{TextStart}}
fh = {{Cb|FREEFILE}}
fh = {{Cb|FREEFILE}}
Line 60: Line 59:
     buffer$ = {{Cl|_READFILE$}}(src$)
     buffer$ = {{Cl|_READFILE$}}(src$)
     {{Cl|IF}} buffer$ = {{Text|<nowiki>""</nowiki>|#FFB100}} {{Cl|AND (boolean)|AND}} {{Cl|_FILEEXISTS}}(src$) = {{Text|0|#F580B1}} {{Cl|THEN}}
     {{Cl|IF}} buffer$ = {{Text|<nowiki>""</nowiki>|#FFB100}} {{Cl|AND (boolean)|AND}} {{Cl|_FILEEXISTS}}(src$) = {{Text|0|#F580B1}} {{Cl|THEN}}
         {{Text|CopyFile$|#55FF55}} = {{Text|<nowiki>"ERROR: Source file doesn't exist."</nowiki>|#FFB100}}
         {{Text|CopyFile$|#55FF55}} = {{Text|<nowiki>"ERROR: Source file not found."</nowiki>|#FFB100}}
     {{Cl|ELSE}}
     {{Cl|ELSE}}
         succ% = {{Cl|_WRITEFILE}}(dst$, buffer$)
         slp% = {{Cl|_INSTRREV}}(dst$, {{Text|<nowiki>"\"</nowiki>|#FFB100}})
         {{Cl|IF}} {{Cl|NOT}} succ% {{Cl|THEN}}
        {{Cl|IF}} slp% = {{Text|0|#F580B1}} {{Cl|THEN}} slp% = {{Cl|_INSTRREV}}(dst$, {{Text|<nowiki>"/"</nowiki>|#FFB100}})
            {{Text|CopyFile$|#55FF55}} = {{Text|<nowiki>"ERROR: Couldn't write destination file."</nowiki>|#FFB100}}
         {{Cl|IF}} slp% > {{Text|0|#F580B1}} {{Cl|THEN}}
            {{Cl|IF}} {{Cl|NOT}} {{Cl|_DIREXISTS}}({{Cl|LEFT$}}(dst$, slp% - {{Text|1|#F580B1}})) {{Cl|THEN}}
                {{Text|CopyFile$|#55FF55}} = {{Text|<nowiki>"ERROR: Destination path not found."</nowiki>|#FFB100}}
                {{Cl|EXIT FUNCTION}}
            {{Cl|END IF}}
         {{Cl|END IF}}
         {{Cl|END IF}}
        {{Cl|_WRITEFILE}} dst$, buffer$
     {{Cl|END IF}}
     {{Cl|END IF}}
{{Cl|END FUNCTION}}
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}
{{Small|Example by RhoSigma}}




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

Latest revision as of 22:43, 25 May 2024

The _WRITEFILE statement writes a string into a new file, overwriting an existing file of the same name. It does OPEN, PUT and CLOSE the file in one run. It's the counterpart to the _READFILE$ function.


Syntax

_WRITEFILE fileSpec$, contents$


Parameters

  • fileSpec$ is the name of the file to write as literal or variable STRING, if required inclusive a full or relative path.
    • To avoid errors you should use _DIREXISTS before using this statement to make sure a desired path exists.
  • contents$ is the literal or variable STRING which its contents shall be written into the file.


Description

  • Sometimes you may be in need to quickly dump a huge amount of data into a file without much fuss, e.g. the results of the pack/unpack functions _DEFLATE$ and _INFLATE$ or when copying a file in conjunction with the _READFILE$ function.
  • In earlier versions of QB64(PE) you had to implement that saving process manually all the time or create a reusable custom FUNCTION for it.
  • Now _WRITEFILE will simplify this, it's mainly for convenience to wrap the following code sequence into one handy statement:
fh = FREEFILE
OPEN fileSpec$ FOR OUTPUT AS #fh: CLOSE #fh
OPEN fileSpec$ FOR BINARY AS #fh
PUT #fh, , contents$
CLOSE #fh


Availability


Examples

Example
Implementing a simple file copy routine using _READFILE$ and _WRITEFILE.
s$ = "Makefile"
d$ = "Makefile - Copy"

r$ = CopyFile$(s$, d$)

IF r$ = "" THEN
    PRINT "Sucessfully copied '"; s$; "' to '"; d$; "'."
ELSE
    PRINT r$
END IF

END

FUNCTION CopyFile$ (src$, dst$)
    CopyFile$ = "" 'empty = success, otherwise error message
    buffer$ = _READFILE$(src$)
    IF buffer$ = "" AND _FILEEXISTS(src$) = 0 THEN
        CopyFile$ = "ERROR: Source file not found."
    ELSE
        slp% = _INSTRREV(dst$, "\")
        IF slp% = 0 THEN slp% = _INSTRREV(dst$, "/")
        IF slp% > 0 THEN
            IF NOT _DIREXISTS(LEFT$(dst$, slp% - 1)) THEN
                CopyFile$ = "ERROR: Destination path not found."
                EXIT FUNCTION
            END IF
        END IF
        _WRITEFILE dst$, buffer$
    END IF
END FUNCTION
Example by RhoSigma


See also



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