How to resolve the algorithm Truncate a file step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Truncate a file step by step in the XPL0 programming language

Table of Contents

Problem Statement

Truncate a file to a specific length.   This should be implemented as a routine that takes two parameters: the filename and the required file length (in bytes).

Truncation can be achieved using system or library calls intended for such a task, if such methods exist, or by creating a temporary file of a reduced size and renaming it, after first deleting the original file, if no other method is available.   The file may contain non human readable binary data in an unspecified format, so the routine should be "binary safe", leaving the contents of the untruncated part of the file unchanged. If the specified filename does not exist, or the provided length is not less than the current file length, then the routine should raise an appropriate error condition. On some systems, the provided file truncation facilities might not change the file or may extend the file, if the specified length is greater than the current length of the file. This task permits the use of such facilities.   However, such behaviour should be noted, or optionally a warning message relating to an non change or increase in file size may be implemented.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Truncate a file step by step in the XPL0 programming language

Source code in the xpl0 programming language

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);
]

  

You may also check:How to resolve the algorithm Pig the dice game step by step in the C++ programming language
You may also check:How to resolve the algorithm Catalan numbers step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Leonardo numbers step by step in the Ada programming language
You may also check:How to resolve the algorithm Elementary cellular automaton step by step in the Nim programming language
You may also check:How to resolve the algorithm Unicode strings step by step in the Rust programming language