How to resolve the algorithm Remove lines from a file step by step in the Fortran 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 Fortran 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 Fortran programming language
Source code in the fortran programming language
SUBROUTINE CROAK(GASP) !Something bad has happened.
CHARACTER*(*) GASP !As noted.
WRITE (6,*) "Oh dear. ",GASP !So, gasp away.
STOP "++ungood." !Farewell, cruel world.
END !No return from this.
SUBROUTINE FILEHACK(FNAME,IST,N)
CHARACTER*(*) FNAME !Name for the file.
INTEGER IST !First record to be omitted.
INTEGER N !Number of records to be omitted.
INTEGER ENUFF,L !Some lengths.
PARAMETER (ENUFF = 66666)!Surely?
CHARACTER*(ENUFF) ALINE !But not in general...
INTEGER NREC !A counter.
INTEGER F,T !Mnemonics for file unit numbers.
PARAMETER (F=66,T=67) !These should do.
LOGICAL EXIST
IF (FNAME.EQ."") CALL CROAK("Blank file name!")
IF (IST.LE.0) CALL CROAK("First record must be positive!")
IF (N.LE.0) CALL CROAK("Remove count must be positive!")
INQUIRE(FILE = FNAME, EXIST = EXIST) !This mishap is frequent, so attend to it.
IF (.NOT.EXIST) CALL CROAK("Can't find a file called "//FNAME) !Tough love.
OPEN (F,FILE=FNAME,STATUS="OLD",ACTION="READ",FORM="FORMATTED") !Grab the source file.
OPEN (T,STATUS="SCRATCH",FORM="FORMATTED") !Request a temporary file.
NREC = 0 !Number of records read so far.
Copy the desired records to a temporary file.
10 READ (F,11,END = 20) L,ALINE(1:MIN(L,ENUFF)) !Minimal protection.
11 FORMAT (Q,A) !Obviously, Q = # of characters to come, A = their format.
IF (L.GT.ENUFF) CALL CROAK("Ow! Lengthy record!!")
NREC = NREC + 1 !If we're here. we've read a record.
IF (NREC.LT.IST .OR. NREC.GE.IST + N) WRITE (T,12) ALINE(1:L) !A desired record?
12 FORMAT (A) !No character count is explicitly specified.
GO TO 10 !Keep on thumping.
Convert from input to output...
20 IF (NREC.LT.IST + N) CALL CROAK("Insufficient records!") !Finished ignoring records?
REWIND T !Not CLOSE! That would discard the file!
CLOSE(F) !The source file still exists.
OPEN (F,FILE=FNAME,FORM="FORMATTED", !But,
1 ACTION="WRITE",STATUS="REPLACE") !This dooms it!
Copy from the temporary file.
21 READ (T,11,END = 30) L,ALINE(1:L) !All records are not longer than ALINE.
WRITE (F,12) ALINE(1:L) !Out it goes.
GO TO 21 !Keep on thumping.
Completed.
30 CLOSE(T) !Abandon the temporary file.
CLOSE(F) !Finished with the source file.
END !Done.
PROGRAM CHOPPER
CALL FILEHACK("foobar.txt",1,2)
END
CHARACTER*42 FUNCTION ERRORWORDS(IT) !Look for an explanation. One day, the system may offer coherent messages.
Curious collection of encountered codes. Will they differ on other systems?
Compaq's compiler was taken over by unintel; http://software.intel.com/sites/products/documentation/hpc/compilerpro/en-us/fortran/lin/compiler_f/bldaps_for/common/bldaps_rterrs.htm
contains a schedule of error numbers that matched those I'd found for Compaq, and so some assumptions are added.
Copying all (hundreds!) is excessive; these seem possible for the usage so far made of error diversion.
Compaq's compiler interface ("visual" blah) has a help offering, which can provide error code information.
Compaq messages also appear in http://cens.ioc.ee/local/man/CompaqCompilers/cf/dfuum028.htm#tab_runtime_errors
Combines IOSTAT codes (file open, read etc) with STAT codes (allocate/deallocate) as their numbers are distinct.
Completeness and context remains a problem. Excess brevity means cause and effect can be confused.
INTEGER IT !The error code in question.
INTEGER LASTKNOWN !Some codes I know about.
PARAMETER (LASTKNOWN = 26) !But only a few, discovered by experiment and mishap.
TYPE HINT !For them, I can supply a table.
INTEGER CODE !The code number. (But, different systems..??)
CHARACTER*42 EXPLICATION !An explanation. Will it be te answer?
END TYPE HINT !Simple enough.
TYPE(HINT) ERROR(LASTKNOWN) !So, let's have a collection.
PARAMETER (ERROR = (/ !With these values.
1 HINT(-1,"End-of-file at the start of reading!"), !From examples supplied with the Compaq compiler involving IOSTAT.
2 HINT( 0,"No worries."), !Apparently the only standard value.
3 HINT( 9,"Permissions - read only?"),
4 HINT(10,"File already exists!"),
5 HINT(17,"Syntax error in NameList input."),
6 HINT(18,"Too many values for the recipient."),
7 HINT(19,"Invalid naming of a variable."),
8 HINT(24,"Surprise end-of-file during read!"), !From example source.
9 HINT(25,"Invalid record number!"),
o HINT(29,"File name not found."),
1 HINT(30,"Unavailable - exclusive use?"),
2 HINT(32,"Invalid fileunit number!"),
3 HINT(35,"'Binary' form usage is rejected."), !From example source.
4 HINT(36,"Record number for a non-existing record!"),
5 HINT(37,"No record length has been specified."),
6 HINT(38,"I/O error during a write!"),
7 HINT(39,"I/O error during a read!"),
8 HINT(41,"Insufficient memory available!"),
9 HINT(43,"Malformed file name."),
o HINT(47,"Attempting a write, but read-only is set."),
1 HINT(66,"Output overflows single record size."), !This one from experience.
2 HINT(67,"Input demand exceeds single record size."), !These two are for unformatted I/O.
3 HINT(151,"Can't allocate: already allocated!"), !These different numbers are for memory allocation failures.
4 HINT(153,"Can't deallocate: not allocated!"),
5 HINT(173,"The fingered item was not allocated!"), !Such as an ordinary array that was not allocated.
6 HINT(179,"Size exceeds addressable memory!")/))
INTEGER I !A stepper.
DO I = LASTKNOWN,1,-1 !So, step through the known codes.
IF (IT .EQ. ERROR(I).CODE) GO TO 1 !This one?
END DO !On to the next.
1 IF (I.LE.0) THEN !Fail with I = 0.
ERRORWORDS = I8FMT(IT)//" is a novel code!" !Reveal the mysterious number.
ELSE !But otherwise, it is found.
ERRORWORDS = ERROR(I).EXPLICATION !And these words might even apply.
END IF !But on all systems?
END FUNCTION ERRORWORDS !Hopefully, helpful.
You may also check:How to resolve the algorithm Special variables step by step in the Arturo programming language
You may also check:How to resolve the algorithm Password generator step by step in the Phix programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Swift programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the LOLCODE programming language
You may also check:How to resolve the algorithm Doomsday rule step by step in the Haskell programming language