Open the file in read mode ,keep it in @arr.
Close the file handler.
Open it again in write mode.
Iterate @arr and apply logic
Write it to file
Close the file handler | [reply] |
You will have to use 'tell' and 'seek'. 'tell' gives the current cursor position and 'seek' can be used to set the filehandle position.
| [reply] |
No, this is a wrong idea.
seek() and tell() normally have no role at all when
processing a text file. That is because these functions
work on byte offsets, not line offsets.
And text lines usually have a variable number of bytes per line.
In general it is not possible to "seek to line 4" because we
don't know the number of bytes from the beginning of the file
that line 4 starts at. In addition, if we could seek to "line 4",
there is no way to edit that line to be either smaller or bigger.
A disk file is like an old fashioned cassette tape. It is a
stream of bits. There is no way to either: insert extra bits
or delete bits from this stream of bits. With few exceptions,
a disk file is a "read only" string of bits.
To 'take out a song' or 'replace a song' on
a cassette tape, you have to make a new cassette and copy the
songs from the 'old cassette' to the "new cassette' and make
adjustments during the copy. So it is with disk files.
-Open the read_file_name
-Open a temp_file for writing
-Write to temp_file by reading "read_file name"
and doing some edits(copy,delete(don't copy),modify).
-close the read_file_name
-rename the read_file_name as a .BAK file
-close temp_file_name
-rename temp_file_name to the original name (read_file)
Part of the idea is to maintain at all times a "way back", even if the program crashes.
I finished (I hope) a fight with MS update that required at the end of the day, 3 re-boots of my machine. This thing insisted upon re-installing and re-booting the same thing 3 times!
My point is that even if your program works correctly, it could happen that it is terminated for reasons outside of your control. Make it as low a probability as you can that a file "gets lost".
| [reply] |