Jump to content

Write entire file: Difference between revisions

→‎{{header|Phix}}: completely redone with newer/simpler routines, syntax coloured, plus write_file() got deprecated, marked js incompatible
(Write entire file in various BASIC dialents)
(→‎{{header|Phix}}: completely redone with newer/simpler routines, syntax coloured, plus write_file() got deprecated, marked js incompatible)
Line 743:
 
=={{header|Phix}}==
<!--<lang Phix>(notonline)-->
Deep in the heart of the compiler itself, after much huffing and puffing, the following code can be found:
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<lang Phix>fn = open(outfile,"wb")
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">write_lines</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"file.txt"</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"line 1\nline 2\n"</span><span style="color: #0000FF;">})</span>
...
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"error"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
string img = repeat(' ',SizeOfImage)
<!--</lang>-->
...
lines can also be {"line 1","line 2"} or other multiline/triplequoted text.<br>
SetCheckSum(img,SizeOfImage)
See also open(), puts(), printf(), close(), temp_file(), create_directory()...
puts(fn,img)
close(fn)</lang>
Obviously as that can successfully write a binary executable, simple strings are no problem, except that when dealing with text you would normally want automatic line ending conversion enabled, so drop the 'b' (binary) mode option, ie the "wb" above should be just "w".
 
The distribution also includes the file builtins\writefile.e which declares
<lang Phix>global function write_file(object file, sequence data, integer as_text = BINARY_MODE, integer encoding = ANSI, integer with_bom = 1)</lang>
which is intended to be a one-line call with full unicode support, however as yet it is neither properly documented nor adequately tested.
 
=={{header|PHP}}==
7,820

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.