How to resolve the algorithm Remove lines from a file step by step in the Amazing Hopper programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Remove lines from a file step by step in the Amazing Hopper programming language

Table of Contents

Problem Statement

Remove a specific line or a number of lines from a file. This should be implemented as a routine that takes three parameters (filename, starting line, and the number of lines to be removed). For the purpose of this task, line numbers and the number of lines start at one, so to remove the first two lines from the file foobar.txt, the parameters should be: foobar.txt, 1, 2 Empty lines are considered and should still be counted, and if the specified line is empty, it should still be removed. An appropriate message should appear if an attempt is made to remove lines beyond the end of the file.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Remove lines from a file step by step in the Amazing Hopper programming language

Source code in the amazing programming language

#include 

main:
   .ctrlc
   fd=0,fw=0
   fopen(OPEN_READ,"archivo.txt")(fd)
   if file error? 
      {"no pude abrir el archivo de lectura: "}
      jsub(show error)
   else
      fcreate(CREATE_NORMAL,"archivoTmp.txt")(fw)
      if file error? 
          {"no pude crear el archivo para escritura: "}
          jsub(show error)
      else
          get arg number(2,desde),  // from line
          get arg number(3,hasta),  // to line

          line read=0
          while( not(feof(fd)))
             fread line(1000)(fd), ++line read
             if(not( {line read} between(desde, hasta)))
                 {"\n"}cat,writeline(fw)
             endif 
          wend
      endif
   endif
   fclose(fw)
   fclose(fd)
   system("mv archivoTmp.txt archivo.txt")
exit(0)
.locals
show error:
  file error, println
back

  

You may also check:How to resolve the algorithm Truncate a file step by step in the Ruby programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Scala programming language
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the BQN programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Inform 7 programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Liberty BASIC programming language