Remove lines from a file: Difference between revisions

Added XPL0 example.
(added Arturo)
(Added XPL0 example.)
Line 3,208:
Line 8
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">def LF=$0A, EOF=$1A;
 
proc EatLine; \Read and discard a text line from input file
int Ch;
[repeat Ch:= ChIn(3);
if Ch = EOF then
[Text(0, "Attempted to remove a line beyond EOF"); exit];
until Ch = LF;
];
 
proc CopyLine; \Read and write a text line
int Ch;
[repeat Ch:= ChIn(3);
if Ch = EOF then
[Text(0, "Attempted to copy a line beyond EOF"); exit];
ChOut(3, Ch);
until Ch = LF;
];
 
proc RemoveLines(FileName, StartLine, NumLines);
int FileName, StartLine, NumLines;
int N, Ch, IH, OH;
[IH:= FOpen(FileName, 0);
FSet(IH, ^i); \open specified file for input
OpenI(3);
 
OH:= FOpen("file.tmp", 1);
FSet(OH, ^o); \open temporary file for output
OpenO(3);
 
for N:= 1 to StartLine-1 do CopyLine; \copy lines up to start of Start line
for N:= 1 to NumLines do EatLine; \discard NumLines
loop [Ch:= ChIn(3); \copy remainder of input file
if Ch = EOF then quit;
ChOut(3, Ch);
];
Close(3);
FClose(OH); FClose(IH);
 
OH:= FOpen("foobar.txt", 1);
FSet(OH, ^o); \open file for output
OpenO(3);
 
IH:= FOpen("file.tmp", 0);
FSet(IH, ^i); \open temp file for input
OpenI(3);
 
loop [Ch:= ChIn(3); \copy file
if Ch = EOF then quit;
ChOut(3, Ch);
];
Close(3);
];
 
RemoveLines("foobar.txt", 1, 2)
]</syntaxhighlight>
 
=={{header|zkl}}==
296

edits