inserting text into a file

Is there a way to write to a file starting at some point inside and inserting the new text instead of overwriting? If there isn't a function that can do that directly, is it possible to read the part of the file from the point I want to insert at on, delete that, append the new text to the file, and then put back the part that I took out? How would I do that?

Comments

  • : Is there a way to write to a file starting at some point inside and
    : inserting the new text instead of overwriting?
    Not that I know of, no. Technically this is not do-able as the data that is physically on the disk in front of where you want to put the new data needs to be shifted. So you need to do something like what you've suggested below...

    : If there isn't a function that can do that directly, is it possible
    : to read the part of the file from the point I want to insert at on,
    : delete that, append the new text to the file, and then put back the
    : part that I took out? How would I do that?
    It's often easier to read the whole file in, manipulate it in memory and then completely overwrite the original with the new data, unless you are dealing with a big file, or the inserts a regularly near the end. Are you working with data where you want to insert at a particular line? If so, I suggest you use the operator in array context to read the whole file in:-

    my @wholefile = ;

    Then loop through each element until you find the line you want to insert after, writing each one to the file. When you find the one you want to insert after, write that and then write whatever is new that you need to. The write the rest of the data.

    If that doesn't suit you, read through the file however you want to find the insert potition, then read in and store all the data till the end of the file (if you call read with a length bigger than what is available to read, it will only read till the end of the file). Then you can use seek to get to the point you want to write to, print the data you want to the file, and then print the stuff you read in.

    TMTOWTDI, but if you want me (or anyone else who feels like posting a reply ;-)) to elaborate on any particular approach just post and say so.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • [b][red]This message was edited by StatusX292 at 2003-6-10 5:47:58[/red][/b][hr]
    Thanks for the help. I know this is unrelated, but I didn't think it was worth starting a new post for. How do I get the current date in a CGI perl program? What format is it in? I hate asking this type of question, so I guess my last one is, where can I find a list of built in functions like this one and how they work? Thanks alot. Oh, and one more thing before I forget. This is the same type of question I hate asking. What is an easy way to get the size of an array? Thanks again.


  • : Thanks for the help. I know this is unrelated, but I didn't think
    : it was worth starting a new post for. How do I get the current date
    : in a CGI perl program? What format is it in?
    Most portable way is to use localtime(time). You can do it like this:-
    [code]my $timedate = localtime(time);[/code]
    Or you can get the individual bits of the date like this:-
    [code]($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);[/code]
    If you want the time at GMT, use gmtime instead of localtime.

    : I hate asking this type of question, so I guess my last one is,
    : where can I find a list of built in functions like this one and how
    : they work? Thanks alot.
    Feel free to ask questions, that's what the board is for. Perl documentation is available at:-
    http://www.perldoc.com/

    : Oh, and one more thing before I forget. This is the same type of
    : question I hate asking. What is an easy way to get the size of an
    : array? Thanks again.
    I'm guessing you mean the number of elements? If so, you simply evaluate the @array in scalar context. For example:-

    [code]$numelements = @array;[/code]

    If you have have to force scalar context, which occasionally may happen...

    [code]$numelements = scalar @array;[/code]

    Three questions, three answers. If you're really lucky, they might be the right ones. Whatever "right" is defined as... :-)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories