dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: Random access files in Perl -How do you do it?

  1. #1
    Join Date
    Jan 2003
    Location
    West Sussex, United Kingdom
    Posts
    12

    Random access files in Perl -How do you do it?

    I am trying to write (and read) fixed length records (400 bytes) from a file in a random fashion.
    ie if I need to write to record no 17, the byte address would be 17 * 400
    I cannot find the appropriate perl instruction (or I cannot make it work). I can make an individual record write
    to the correct address, but in doing so it deletes all the previous ones!

    Here is the program - what am I doing wrong?


    $Filnam="log.dat";
    open (LOG, ">$Filnam");

    $struct = "l,l,l,a388";
    @dat0 = (1,0,2,"This record 0 spare part");
    @dat1 =(99,100,66,"This record 1");
    $buffer = pack($struct,@dat0);
    $buf2= pack($struct,@dat1);
    seek(LOG,0,0);
    syswrite(LOG,$buffer);
    close (LOG);

    #when you close it here, the first write disappears from the file when you re-write next record

    open (LOG, ">$Filnam");
    seek(LOG,400,0);
    syswrite(LOG,$buf2);

    # this appears in the right place in the file but record 0 has gone when you look at the file

    close (LOG);

    WHAT am I doing wrong?

    Thanks Don

  2. #2
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    The reason you're losing the previous records is because your open mode truncate's the entire file and then opens it for output. It's also not a good idea ot mix sysread/syswrite with print, write, seek, tell, or eof. Your seek your be done with sysseek, or your syswrite should be done with print.

  3. #3
    celebguy_dv Guest

  4. #4
    Join Date
    Jan 2003
    Location
    West Sussex, United Kingdom
    Posts
    12
    Originally posted by jeffmott
    The reason you're losing the previous records is because your open mode truncate's the entire file and then opens it for output. It's also not a good idea ot mix sysread/syswrite with print, write, seek, tell, or eof. Your seek your be done with sysseek, or your syswrite should be done with print.
    Thanks for your help - I have now realised that perl does not really like random access! So I have now re-written so that I manipulate the data by reading the whole lot of records into a string and then manipulate the string (using subst) and then write the whole lot back with a print.

    Is this the best way?

    Cherrs Don (and thanks again)

  5. #5
    Join Date
    Nov 2002
    Location
    NY, USA
    Posts
    731
    Perl can do fixed length data records just fine, you just don't want to mix sysread, sysseek, syswrite with read, seek, and print. If you're still having problems, post what code you now have and we'll see what might be going wrong.

  6. #6
    Join Date
    Jan 2003
    Location
    West Sussex, United Kingdom
    Posts
    12
    Originally posted by jeffmott
    Perl can do fixed length data records just fine, you just don't want to mix sysread, sysseek, syswrite with read, seek, and print. If you're still having problems, post what code you now have and we'll see what might be going wrong.
    Thanks for your help - code seems to be working fine - still testing it at the moment, will keep your offer in mind!.

    Have a good day - its 7.50 am here, but prpb much earlier where you are!

    Don

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles