@H_404_6@1. 打开句柄,设置为bin模式 @H_404_6@open(GIF,$gifname) or dIE "can't open $gifname: $!";
open(GIFOUT,">$gifOutname") or dIE "can't open $gifOutname: $!";
binmode(GIF);
binmode(GIFOUT);
@H_404_6@2. 16机制,10机制,字符转换用pack函数 @H_404_6@
@H_404_6@3. 读写方法 @H_404_6@
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 "4. 文件读写指针 *** 作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." 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.
@H_404_6@ @H_404_6@ @H_404_6@
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 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);@H_404_6@
@H_404_6@举例 @H_404_6@ 总结
以上是内存溢出为你收集整理的perl *** 作二进制文件方法全部内容,希望文章能够帮你解决perl *** 作二进制文件方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)