Home > Archive > PERL Beginners > March 2004 > Using the read function with an offset
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Using the read function with an offset
|
|
| Lolbassett@aol.com 2004-03-30, 1:31 pm |
| Hi!
I'm writing some 68k modules, for disassembly, assembly, reading
instructions, analysing instructions, etc.
The package I'm stuck on is supposed to read a 16bit instruction, and then
break it down into an array, with each element seperated.
So far, I'm trying to read 16bits, at an offset specified by the user, but
for some reason, all I get is more white spaces infront of the first 16bits in
the file.
open SOURCE, "$File" or
die "Cannot open $File\n";
binmode SOURCE;
read SOURCE, $Instr, 2, $Offset;
return $Instr;
I find that if I read from the first byte, I get the first two bytes in my
file, but if I read from the 5, I just get three white spaces, and then the
first two bytes.
Can anybody help?
Thanks,
Lewis AS Bassett
| |
| Bob Showalter 2004-03-30, 1:31 pm |
| Lolbassett@aol.com wrote:
> Hi!
>
> I'm writing some 68k modules, for disassembly, assembly, reading
> instructions, analysing instructions, etc.
>
> The package I'm stuck on is supposed to read a 16bit instruction, and
> then break it down into an array, with each element seperated.
>
> So far, I'm trying to read 16bits, at an offset specified by the
> user, but for some reason, all I get is more white spaces infront of
> the first 16bits in the file.
>
> open SOURCE, "$File" or
> die "Cannot open $File\n";
>
> binmode SOURCE;
>
> read SOURCE, $Instr, 2, $Offset;
Nope. The offset argument to read() specifies the offset in your buffer
where you want the data to be placed. Reading always proceeds from the
file's current position, which you manipulate with the s () function.
You want something like this:
use Fcntl 'SEEK_SET';
s SOURCE, $Offset, SEEK_SET;
read SOURCE, $Instr, 2;
|
|
|
|
|