您的位置:首页 > 其它

perl操作二进制文件方法

2011-08-17 22:09 302 查看
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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: