doness
02-23-2003, 03:45 AM
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
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