Punto has asked for the wisdom of the Perl Monks concerning the following question:

Hi.. I wrote a small db manager and I want to corrupt the files to test it, so I wrote this script:
#!/usr/bin/perl $fsize = -s $ARGV[0]; open (COR, ">>$ARGV[0]"); for (0..$fsize / 100) { seek(COR, rand($fsize), 0); print COR chr(rand(256)); }; close(COR);

I want it to seek to a random point on the file, and write a random byte there.. But it's writing all the random bytes at the end of the file, and not in the middle of it..
Am I using the wrong function to seek or write to the file?

Thanks..

Replies are listed 'Best First'.
Re: corruption.
by jsegal (Friar) on Feb 15, 2002 at 19:03 UTC
    Hi.

    You are opening the file for appending. Try opening it for read-write ("+<" instead of ">>").
    See perlfunc:open for more info.
    Hope this helps.

    -JAS
Re: corruption.
by Rich36 (Chaplain) on Feb 15, 2002 at 18:59 UTC
    It could be because you are using the >> operator in the open statement. >> means append and that's why it's alway showing at the end. I would open the file, read in the contents, then re-write the same file or write to another file that you could do the corruption testing on.
    Rich36
    There's more than one way to screw it up...