Truncate a file: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: syntax coloured, marked p2js incompatible) |
(Added XPL0 example.) |
||
Line 1,571: | Line 1,571: | ||
Contents are still : abcdefghijklm |
Contents are still : abcdefghijklm |
||
</pre> |
</pre> |
||
=={{header|XPL0}}== |
|||
Works for binary files. |
|||
<lang XPL0>int I, Size, FD; |
|||
char C, FN(80), Array; |
|||
[I:= 0; \get file name from command line |
|||
loop [C:= ChIn(8); |
|||
if C = $20 \space\ then quit; |
|||
FN(I):= C; |
|||
I:= I+1; |
|||
]; |
|||
FN(I):= 0; |
|||
Size:= IntIn(8); \get number of bytes from command line |
|||
if Size = 0 then [Text(0, "Length not found (or zero)"); exit 1]; |
|||
Trap(false); \disable abort on errors |
|||
FD:= FOpen(FN, 0); \open specified file for input |
|||
FSet(FD, ^i); |
|||
OpenI(3); |
|||
if GetErr then [Text(0, "File not found"); exit 1]; |
|||
Array:= Reserve(0); \64MB available if no procedures are called |
|||
for I:= 0 to Size-1 do \read specified number of bytes |
|||
[Array(I):= ChIn(3); |
|||
if GetErr then [Text(0, "File is too short"); exit 1]; |
|||
]; \if end of file encountered, show error |
|||
FClose(FD); |
|||
FD:= FOpen(FN, 1); \open file by same name for output |
|||
FSet(FD, ^o); |
|||
OpenO(3); |
|||
if GetErr then [Text(0, "Output error"); exit 1]; |
|||
for I:= 0 to Size-1 do ChOut(3, Array(I)); |
|||
Close(3); |
|||
]</lang> |
|||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |