📄 ch08_16.htm
字号:
<HTML><HEAD><TITLE>Recipe 8.15. Reading Fixed-Length Records (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:38:51Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch08_01.htm"TITLE="8. File Contents"><LINKREL="prev"HREF="ch08_15.htm"TITLE="8.14. Reading a String from a Binary File"><LINKREL="next"HREF="ch08_17.htm"TITLE="8.16. Reading Configuration Files"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch08_15.htm"TITLE="8.14. Reading a String from a Binary File"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 8.14. Reading a String from a Binary File"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch08_01.htm"TITLE="8. File Contents"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch08_17.htm"TITLE="8.16. Reading Configuration Files"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 8.16. Reading Configuration Files"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch08-chap08_reading_4">8.15. Reading Fixed-Length Records</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch08-pgfId-1358">Problem<ACLASS="indexterm"NAME="ch08-idx-1000004699-0"></A><ACLASS="indexterm"NAME="ch08-idx-1000004699-1"></A><ACLASS="indexterm"NAME="ch08-idx-1000004699-2"></A><ACLASS="indexterm"NAME="ch08-idx-1000004699-3"></A><ACLASS="indexterm"NAME="ch08-idx-1000004699-4"></A></A></H3><PCLASS="para">You want to read a file whose records have a fixed length.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch08-pgfId-1364">Solution</A></H3><PCLASS="para">Use <CODECLASS="literal">read</CODE> and <CODECLASS="literal">unpack</CODE>:</P><PRECLASS="programlisting"># $RECORDSIZE is the length of a record, in bytes.# $TEMPLATE is the unpack template for the record# FILE is the file to read from# @FIELDS is an array, one element per fielduntil ( eof(FILE) ) { read(FILE, $record, $RECORDSIZE) == $RECORDSIZE or die "short read\n"; @FIELDS = unpack($TEMPLATE, $record);}</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch08-pgfId-1390">Discussion</A></H3><PCLASS="para">Because the file in question is not a text file, you can't use <CODECLASS="literal"><FH></CODE> or IO:: modules' <CODECLASS="literal">getline</CODE> method to read in records. Instead, you must simply <CODECLASS="literal">read</CODE> a particular number of bytes into a buffer. This buffer then contains one record's data, which you decode using <CODECLASS="literal">unpack</CODE> with the right format.</P><PCLASS="para">For binary data, the catch is often determining the right format. If you're reading data written by a C program, this can mean peeking at C include files or manpages describing the structure layout, and this requires knowledge of C. It also requires that you become unnaturally chummy with your C compiler, because otherwise it's hard to predict field padding and alignment (such as the <CODECLASS="literal">x2</CODE> in the format used in <ACLASS="xref"HREF="ch08_19.htm"TITLE="Program: tailwtmp">Recipe 8.18</A>). If you're lucky enough to be on a Berkeley Unix system or a system supporting <EMCLASS="emphasis">gcc</EM>, then you may be able to use the <EMCLASS="emphasis">c2ph</EM> tool distributed with Perl to cajole your C compiler into helping you with this.</P><PCLASS="para">The <EMCLASS="emphasis">tailwtmp</EM> program at the end of this chapter uses the format described in <EMCLASS="emphasis">utmp </EM>(5) under Linux and works on its <EMCLASS="emphasis">/var/log/wtmp</EM> and <EMCLASS="emphasis">/var/run/utmp</EM> files. Once you commit to working in a binary format, machine dependencies creep in fast. It probably won't work unaltered on your system, but the procedure is still illustrative. Here is the relevant layout from the C include file on Linux:</P><PRECLASS="programlisting">#define UT_LINESIZE 12#define UT_NAMESIZE 8#define UT_HOSTSIZE 16struct utmp { /* here are the pack template codes */ short ut_type; /* s for short, must be padded */ pid_t ut_pid; /* i for integer */ char ut_line[UT_LINESIZE]; /* A12 for 12-char string */ char ut_id[2]; /* A2, but need x2 for alignment */ time_t ut_time; /* l for long */ char ut_user[UT_NAMESIZE]; /* A8 for 8-char string */ char ut_host[UT_HOSTSIZE]; /* A16 for 16-char string */ long ut_addr; /* l for long */};</PRE><PCLASS="para">Once you figure out the binary layout, feed that (in this case, <CODECLASS="literal">"s</CODE> <CODECLASS="literal">x2</CODE> <CODECLASS="literal">i</CODE> <CODECLASS="literal">A12</CODE> <CODECLASS="literal">A2</CODE> <CODECLASS="literal">x2</CODE> <CODECLASS="literal">l</CODE> <CODECLASS="literal">A8</CODE> <CODECLASS="literal">A16</CODE> <CODECLASS="literal">l"</CODE>) to <CODECLASS="literal">pack</CODE> with an empty field list to determine the record's size. Remember to check the return value of <CODECLASS="literal">read</CODE> when you read in your record to make sure you got back the number of bytes you asked for.</P><PCLASS="para">If your records are text strings, use the <CODECLASS="literal">"a"</CODE> or <CODECLASS="literal">"A"</CODE> unpack templates.</P><PCLASS="para">Fixed-length records are useful in that the <EMCLASS="emphasis">n</EM> th record begins at byte offset <CODECLASS="literal">SIZE</CODE> <CODECLASS="literal">*</CODE> <CODECLASS="literal">(</CODE><EMCLASS="emphasis">n</EM><CODECLASS="literal">-1)</CODE> in the file, where <CODECLASS="literal">SIZE</CODE> is the size of a single record. See the indexing code in <ACLASS="xref"HREF="ch08_09.htm"TITLE="Reading a Particular Line in a File">Recipe 8.8</A> for an example of this.<ACLASS="indexterm"NAME="ch08-idx-1000004702-0"></A><ACLASS="indexterm"NAME="ch08-idx-1000004702-1"></A><ACLASS="indexterm"NAME="ch08-idx-1000004702-2"></A><ACLASS="indexterm"NAME="ch08-idx-1000004702-3"></A><ACLASS="indexterm"NAME="ch08-idx-1000004702-4"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch08-pgfId-1000004569">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">unpack</CODE>, <CODECLASS="literal">pack</CODE>, and <CODECLASS="literal">read</CODE> functions in <ICLASS="filename">perlfunc </I>(1) and in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; <ACLASS="xref"HREF="ch01_02.htm"TITLE="Accessing Substrings">Recipe 1.1</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch08_15.htm"TITLE="8.14. Reading a String from a Binary File"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 8.14. Reading a String from a Binary File"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch08_17.htm"TITLE="8.16. Reading Configuration Files"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 8.16. Reading Configuration Files"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">8.14. Reading a String from a Binary File</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">8.16. Reading Configuration Files</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -