perl操作二进制文件方法


1. 打开句柄,设置为bin模式

open(GIF, $gifname)         or die "can't open $gifname: $!";
open(GIFOUT, ">$gifOutname")         or die "can't open $gifOutname: $!";
binmode(GIF);
binmode(GIFOUT);

2. 16机制,10机制,字符转换用pack函数


3. 读写方法

sysread FILEHANDLE,SCALAR,LENGTH,OFFSET 

sysread FILEHANDLE,SCALAR,LENGTH 
Attempts to read LENGTH bytes of data into variable SCALAR from the specified FILEHANDLE, using the read(2). It bypasses buffered IO, so mixing this with other kinds of reads, print, write, seek, tell, or eof can cause confusion because the perlio or stdio layers usually buffers data. Returns the number of bytes actually read, 0 at end of file, or undef if there was an error (in the latter case $! is also set). SCALAR will be grown or shrunk so that the last byte actually read is the last byte of the scalar after the read.

An OFFSET may be specified to place the read data at some place in the string other than the beginning. A negative OFFSET specifies placement at that many characters counting backwards from the end of the string. A positive OFFSET greater than the length of SCALAR results in the string being padded to the required size with "\0" bytes before the result of the read is appended.

There is no syseof() function, which is ok, since eof() doesn't work well on device files (like ttys) anyway. Use sysread() and check for a return value for 0 to decide whether you're done.

Note that if the filehandle has been marked as :utf8 Unicode characters are read instead of bytes (the LENGTH, OFFSET, and the return value of sysread() are in Unicode characters). The :encoding(...) layer implicitly introduces the :utf8 layer. See binmode, open, and the open pragma, open.
syswrite FILEHANDLE,SCALAR,LENGTH,OFFSET 

syswrite FILEHANDLE,SCALAR,LENGTH 
syswrite FILEHANDLE,SCALAR 
Attempts to write LENGTH bytes of data from variable SCALAR to the specified FILEHANDLE, using write(2). If LENGTH is not specified, writes whole SCALAR. It bypasses buffered IO, so mixing this with reads (other than sysread()), print, write, seek, tell, or eof may cause confusion because the perlio and stdio layers usually buffer data. Returns the number of bytes actually written, or undef if there was an error (in this case the errno variable $! is also set). If the LENGTH is greater than the data available in the SCALAR after the OFFSET, only as much data as is available will be written.

An OFFSET may be specified to write the data from some part of the string other than the beginning. A negative OFFSET specifies writing that many characters counting backwards from the end of the string. If SCALAR is of length zero, you can only use an OFFSET of 0.

WARNING: If the filehandle is marked :utf8 , Unicode characters encoded in UTF-8 are written instead of bytes, and the LENGTH, OFFSET, and return value of syswrite() are in (UTF8-encoded Unicode) characters. The :encoding(...) layer implicitly introduces the :utf8 layer. Alternately, if the handle is not marked with an encoding but you attempt to write characters with code points over 255, raises an exception. See binmode, open, and the open pragma, open.



4. 文件读写指针操作

sysseek FILEHANDLE,POSITION,WHENCE 

Sets FILEHANDLE's system position in bytes using lseek(2). FILEHANDLE may be an expression whose value gives the name of the filehandle. The values for WHENCE are 0 to set the new position to POSITION; 1 to set the it to the current position plus POSITION; and 2 to set it to EOF plus POSITION, typically negative.

Note the in bytes: even if the filehandle has been set to operate on characters (for example by using the :encoding(utf8) I/O layer), tell() will return byte offsets, not character offsets (because implementing that would render sysseek() unacceptably slow).

sysseek() bypasses normal buffered IO, so mixing it with reads other than sysread (for example <> or read()) print, write, seek, tell, or eof may cause confusion.

For WHENCE, you may also use the constants SEEK_SET , SEEK_CUR , and SEEK_END (start of the file, current position, end of the file) from the Fcntl module. Use of the constants is also more portable than relying on 0, 1, and 2. For example to define a "systell" function:

    use Fcntl 'SEEK_CUR';    sub systell { sysseek($_[0], 0, SEEK_CUR) }Returns the new position, or the undefined value on failure. A position of zero is returned as the string "0 but true" ; thus sysseek returns true on success and false on failure, yet you can still easily determine the new position.


举例

use strict;
my $gifname = "C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\1.jpg";
my $gifOutname = "d:\\1111.dat";
my $buff;
open(GIF, $gifname)         or die "can't open $gifname: $!";
open(GIFOUT, ">$gifOutname")         or die "can't open $gifOutname: $!";
binmode(GIF);
binmode(GIFOUT);
read(GIF, $buff, 1);
my $hex = unpack("H*", $buff);
print "ok" if $hex eq "ff";
$hex = "00";
my $outVar = pack("H*", $hex);
print GIFOUT $outVar;
close(GIF);
close(GFIOUT);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值