⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch9.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 5 页
字号:
fuNCtion is sent directly to the <TT>MESSAGE.LOG</TT>file using the connection established by the <TT>open()</TT>fuNCtion.<P>In this example, the <TT>print()</TT>fuNCtion uses the first parameter as a file handle and the secondparameter as a list of things to print. You can find more informationabout printing in the section, &quot;Example: Printing Revisited,&quot;later in this chapter.<P>If you needed to add something to the end of the MESSAGE.LOG file,you use <TT>&gt;&gt;</TT> as the filename prefix when opening the file. For example:<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Call the </I><TT><I>open()</I></TT><I>fuNCtion to open the </I><TT><I>MESSAGE.LOG</I></TT><I>file for appending with </I><TT><I>LOGFILE</I></TT><I>as the file handle. If the file does not exist, it will be created;otherwise, anything printed to </I><TT><I>LOGFILE</I></TT><I>will be added to the end of the file.<BR>Send a message to the </I><TT><I>MESSAGE.LOG</I></TT><I>file.<BR>Send a message to the </I><TT><I>MESSAGE.LOG</I></TT><I>file.<BR>Close the file.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>if (open(LOGFILE, &quot;&gt;&gt;message.log&quot;)) {    print LOGFILE (&quot;This is message number 3.\n&quot;);    print LOGFILE (&quot;This is message number 4.\n&quot;);    close(LOGFILE);}</PRE></BLOCKQUOTE><P>Now, when <TT>MESSAGE.LOG</TT> isviewed, it contains the following lines:<BLOCKQUOTE><PRE>This is message number 1.This is message number 2.This is message number 3.This is message number 4.</PRE></BLOCKQUOTE><H3><A NAME="ExampleBinaryFiles">Example: Binary Files</A></H3><P>When you need to work with data files, you will need to know whatbinary mode is. There are two major differeNCes between binarymode and text mode:<UL><LI>In DOS and Windows, line endings are indicated by two characters-thenewline and carriage return characters. When in text mode, thesecharacters are input as a single character, the newline character.In binary mode, both characters can be read by your program. UNIXsystems only use one character, the newline, to indicate lineendings.<LI>In DOS and Windows, the end of file character is 26. Whena byte with this value is read in text mode, the file is consideredended and your program cannot read any more information from thefile. UNIX considers the end-of-file character to be 4. For bothoperating systems, binary mode will let the end-of-file characterbe treated as a regular character.</UL><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>The examples in this section relate to the DOS operating system.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>In order to demonstrate these differeNCes, we'll use a data filecalled BINARY.DAT with the following contents:<BLOCKQUOTE><PRE>010203</PRE></BLOCKQUOTE><P>First, we'll read the file in the default text mode.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize a buffer variable. Both </I><TT><I>read()</I></TT><I>and </I><TT><I>sysread()</I></TT><I>need their buffer variables to be initialized before the fuNCtioncall is executed.<BR>Open the </I><TT><I>BINARY.DAT</I></TT><I>file for reading.<BR>Read the first 20 characters of the file using the </I><TT><I>read()</I></TT><I>fuNCtion.<BR>Close the file.<BR>Create an array out of the characters in the </I><TT><I>$buffer</I></TT><I>variable and iterate over that array using a </I><TT><I>foreach</I></TT><I>loop.<BR>Print the value of the current array element in hexadecimal format.<BR>Print a newline character. The current array element is a newlinecharacter.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 9.8&nbsp;&nbsp;09LST08.PL-Reading a File to Show TextMode Line Endings<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>$buffer = &quot;&quot;;open(FILE, &quot;&gt;binary.dat&quot;);read(FILE, $buffer, 20, 0);close(FILE);foreach (split(//, $buffer)) {    printf(&quot;%02x &quot;, ord($_));    print &quot;\n&quot; if $_ eq &quot;\n&quot;;}</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>30 31 0a30 32 0a30 33 0a</PRE></BLOCKQUOTE><P>This example does a couple of things that haven't been seen yetin this book. The <TT>Read()</TT>fuNCtion is used as an alternative to the line-by-line input donewith the diamond operator. It will read a specified number ofbytes from the input file and assign them to a buffer variable.The fourth parameter specifies an offset at which to start reading.In this example, we started at the beginning of the file.<P>The <TT>split()</TT> fuNCtion in the<TT>foreach</TT> loop breaks a stringinto pieces and places those pieces into an array. The doubleslashes indicate that each character in the string should be anelement of the new array. <P>For more information about the <TT>split()</TT>fuNCtion, see <A HREF="ch5.htm" >Chapter 5</A> &quot;FuNCtions,&quot; and <A HREF="ch10.htm" >Chapter 10</A>,&quot;Regular Expressions.&quot; <P>ONCe the array of characters has been created, the <TT>foreach</TT>loop iterates over the array. The <TT>printf()</TT>statement converts the ordinal value of the character into hexadecimalbefore displaying it. The <I>ordinal</I> value of a characteris the value of the ASCII representation of the character. Forexample, the ordinal value of '0' is 0x30 or 48.<P>The next line, the print statement, forces the output onto a newline if the current character is a newline character. This wasdone simply to make the output display look a little like theinput file. <P>For more information about the <TT>printf()</TT>fuNCtion, see the section, &quot;Example: Printing Revisited,&quot;later in this chapter.<P>Now, let's read the file in binary mode and see how the outputis changed.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize a buffer variable.<BR>Open the </I><TT><I>BINARY.DAT</I></TT><I>file for reading.<BR>Change the mode to binary.<BR>Read the first 20 characters of the file using the </I><TT><I>read()</I></TT><I>fuNCtion.<BR>Close the file.<BR>Create an array out of the characters in the </I><TT><I>$buffer</I></TT><I>variable and iterate over that array using a </I><TT><I>foreach</I></TT><I>loop.<BR>Print the value of the current array element in hexadecimal format.<BR>Print a newline character. The current array element is a newlinecharacter.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 9.9&nbsp;&nbsp;09LST09.PL-Reading a File to Show BinaryMode Line Endings<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>$buffer = &quot;&quot;;open(FILE, &quot;&lt;binary.dat&quot;);binmode(FILE); read(FILE, $buffer, 20, 0);close(FILE);foreach (split(//, $buffer)) {    printf(&quot;%02x &quot;, ord($_));    print &quot;\n&quot; if $_ eq &quot;\n&quot;;}</PRE></BLOCKQUOTE><HR><P>This program displays:<BR><BLOCKQUOTE><PRE>30 31 0d 0a30 32 0d 0a30 33 0d 0a</PRE></BLOCKQUOTE><P>When the file is read in binary mode, you can see that there arereally two characters at the end of every line-the linefeed andnewline characters.<P>Our next example will look at the end-of-file character in bothtext and binary modes. We'll use a data file called <TT>EOF.DAT</TT>with the following contents:<BLOCKQUOTE><PRE>0102&lt;end of file character&gt;03</PRE></BLOCKQUOTE><P>SiNCe the end-of-file character is a non-printing character, itcan't be shown directly. In the spot <TT>&lt;endof file character&gt;</TT> above is really the value 26.<P>Here is the program that you saw previously read the BINARY.DATfile, only this time, it will read EOF.DAT.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize a buffer variable.<BR>Open the </I><TT><I>BINARY.DAT</I></TT><I>file for reading.<BR>Read the first 20 characters of the file using the </I><TT><I>read()</I></TT><I>fuNCtion.<BR>Close the file.<BR>Create an array of out of the characters in the </I><TT><I>$buffer</I></TT><I>variable and iterate over that array using a </I><TT><I>foreach</I></TT><I>loop.<BR>Print the value of the current array element in hexadecimal format.<BR>Print a newline character. The current array element is a newlinecharacter.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 9.10&nbsp;&nbsp;09LST10.PL-Reading a File to Show theText Mode End-of-File Character<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>$buffer = &quot;&quot;;open(FILE, &quot;&lt;eof.dat&quot;);read(FILE, $buffer, 20, 0);close(FILE);foreach (split(//, $buffer)) {    printf(&quot;%02x &quot;, ord($_));    print &quot;\n&quot; if $_ eq &quot;\n&quot;;}</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>30 31 0d 0a30 32 0d 0a</PRE></BLOCKQUOTE><P>The end-of-file character prevents the read() fuNCtion from readingthe third line. If the file is placed into binary mode, the wholefile can be read.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Initialize a buffer variable.<BR>Open the </I><TT><I>BINARY.DAT</I></TT><I>file for reading.<BR>Change the mode to binary.<BR>Read the first 20 characters of the file using the </I><TT><I>read()</I></TT><I>fuNCtion.<BR>Close the file.<BR>Create an array of out of the characters in the </I><TT><I>$buffer</I></TT><I>variable and iterate over that array using a </I><TT><I>foreach</I></TT><I>loop.<BR>Print the value of the current array element in hexadecimal format.<BR>Print a newline character. The current array element is a newlinecharacter.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 9.11&nbsp;&nbsp;09LST11.PL-Reading a File to Show thatBinary Mode Does Not Recognize the End-of-File Character<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>$buffer = &quot;&quot;;open(FILE, &quot;&lt;eof.dat&quot;);binmode(FILE);read(FILE, $buffer, 20, 0);close(FILE);foreach (split(//, $buffer)) {    printf(&quot;%02x &quot;, ord($_));    print &quot;\n&quot; if $_ eq &quot;\n&quot;;}</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>30 31 0d 0a30 32 0d 0a1a 30 33 0d 0a</PRE>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -