osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

how to replace line on particular line in file[no need to write it back whole file again]


Thanks Dennis Lee and Thomas

On Thu, Oct 11, 2018 at 6:32 PM Dennis Lee Bieber <wlfraed at ix.netcom.com>
wrote:

> On Thu, 11 Oct 2018 15:14:49 +0530, Iranna Mathapati
> <iranna.gani28 at gmail.com> declaimed the following:
>
> >Hi Team,
> >
> >How to replace particular line text with  new text on a file
> >i have below code but its writing whole code.
> >
>         <SNIP>
>
>         For C-style streams (which seem to have taken over every operating
> system) in which "lines" are determined by specific characters in the
> stream, you can only "replace" a line IF THE REPLACEMENT IS THE EXACT SAME
> SIZE! If there is any difference in length of the input and output lines,
> you have to read/write everything from the line to the end of the file.
>
>         I emphasize "C-style streams" as there were operating systems that
> did
> support other file organizations natively. Xerox CP-V offered
> "consecutive", "keyed", and "random" file organizations.
>
>         "Consecutive" being the closest to streams -- pure sequentially
> written
> data.
>
>         "Keyed" files used a multi-level ISAM structure -- which is what
> was
> used by the text editor and by the FORTRAN direct-access mode (line/record
> numbers were used as the ISAM key). In these, replacing a line meant
> writing a new line <somewhere> and updating the ISAM headers to point to
> the new line. Periodically one would have to pack the file to reclaim
> unused/freed space.
>
>         "Random" files were an organization in which the OS did /nothing/.
> These files were created as a predetermined maximum size, and were
> allocated as a contiguous section of disk space -- the OS didn't even
> bother tracking blocks. An application basically was given two pieces of
> information: the starting address on disk, and the total length of the
> allocation. ALL data management had to be done by the application.
>
>         Past that, there is the concept of a fixed-length record. ALL
> records
> (lines) in the file having the same predefined maximum length. Internally,
> each record would either have a length datum at the beginning, or use some
> special character to indicate where the end of the record's data really is
> -- but all I/O was performed on the full <maximum length>. Even
> TRSDOS/LS-DOS (TRS-80 Model III/4) supported fixed-length I/O in the OS,
> though the maximum record length was one disk sector (256 bytes; shorter
> records definitions were permitted and the system /did/ pack them into disk
> space rather than wasting the tail of the sector). Fixed length schemes
> typically permit direct access
>
>                 x = read(rec=123)
>
>                 write(y, rec=123)
>
>
>
> --
>         Wulfraed                 Dennis Lee Bieber         AF6VN
>         wlfraed at ix.netcom.com    HTTP://wlfraed.home.netcom.com/
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>