Results 1 to 4 of 4
  1. #1
    Full Member c4's Avatar
    Join Date
    January 18th, 2005
    Posts
    488
    Hi!

    Here is what I want to know:

    I have a data (text) file with many lines. I want to change something in line let's say 100 but leave all the rest intact.

    Is there a quicker way to do this instead of:

    #########

    $myline = 100; # the line I want to edit

    #### Get data

    open( DATA, "data.txt" );
    @data = < DATA >;
    close( DATA );

    $line = $#data + 1;

    #### Now re-writing the file

    open( DATA, ">data.txt" );
    flock( DATA,2 );

    foreach $data_line ( @data ) {
    chomp $data_line;
    if( $line == $myline ) { print DATA "this is my change\n"; }
    else { print DATA "$data_line\n"; }
    }

    close( DATA );

    #############

    If not, how can I make sure that no script writes to the data file from the time I read the data from data.txt to the time I start re-writing the file (and use flock)?


    Thanks in advance!

  2. #2
    Newbie
    Join Date
    January 18th, 2005
    Posts
    10
    What about reading until you get to line100, writing all lines back to a new file,
    and then write the new line to the new file,
    then read and write all remaining lines,
    then move the new file over to the old
    file name. Just one way of doing it fast.
    There's probably heaps of ways of doing it.
    What type of program is it and what os will
    it run on?

    Do you know any VB or do you know any VB
    programmers? I need help writing an animation app. Have a look at...
    http://www.bri.net.au/~plethora

    Geoff Willliams
    plethora@bri.net.au

  3. #3
    Newbie
    Join Date
    January 18th, 2005
    Posts
    10
    Here's another way that might work, just like yours but simplified, see if it does the same thing...

    $myline = 100; # the line I want to edit
    $mynewline = "Hello I am a new line that has been replaced.";

    open( DATA, "data.txt" );
    @data = < DATA >; # Get data
    close( DATA );
    $data[$myline]=$mynewline; # Make the change
    open( DATA, ">data.txt" );
    foreach $line (@data){
    print DATA $line; # Write the data file to disk
    }
    close(DATA);

    You'd have to test it though, I think I left out out /n after the newlinetext goes in.

    But it shows that the file writing and reading should be done a lot quicker. Better to have the swap and the conditional outside of the I/O part.


    Geoff.

  4. #4
    Full Member c4's Avatar
    Join Date
    January 18th, 2005
    Posts
    488
    Hi!

    Thanks for reply! I already got this working pretty much the same way you suggested maybe just a line of code less [img]/infopop/emoticons/icon_smile.gif[/img]


    $myline = 100; # the line I want to edit
    $mynewline = "Hello I am a new line that has been replaced.";

    open( DATA, "data.txt" );
    @data = < DATA >; # Get data
    close( DATA );
    $data[$myline]=$mynewline; # Make the change
    open( DATA, ">data.txt" );
    print DATA @data; # Write the data file to disk
    close(DATA);

    It's the same I'm just not re-writting the file line by line. (Note that print DATA @data; must be used, not print data "@data"; since this second one ads spaces to the beginning of every new line)


    VB programmers? Sorry, can't think of anyone ...

    Regards

    ## UBB = !%#)&$=)"%/!"#&#=?*"!*)=*)#ยจ!"

    [ 08-22-2002: Message edited by: c4 ]

+ Reply to Thread

Similar Threads

  1. Editing exe file for clickbank
    By swecs in forum Programming / Datafeeds / Tools
    Replies: 0
    Last Post: April 2nd, 2009, 08:20 PM
  2. Problems downloading NetShops data file
    By chazzbro in forum Cusimano.com Scripts
    Replies: 5
    Last Post: December 22nd, 2008, 10:07 PM
  3. editing Robots.txt file
    By KODea in forum Search Engine Optimization
    Replies: 5
    Last Post: August 28th, 2006, 03:02 PM
  4. data feed text file name question.
    By steven420 in forum Programming / Datafeeds / Tools
    Replies: 1
    Last Post: August 23rd, 2006, 08:40 PM
  5. Help with New Line data feed
    By hailpear in forum Commission Junction - CJ
    Replies: 4
    Last Post: June 15th, 2005, 10:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •