加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl操作二进制文件方法

发布时间:2020-12-16 00:35:05 所属栏目:大数据 来源:网络整理
导读: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. 读写方


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,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 "" 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,OFFSET 

syswrite FILEHANDLE,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()),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,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.



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,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],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 SettingsAdministratorMy DocumentsMy Pictures1.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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读