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

📄 ch9.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 5 页
字号:

The <TT>open()</TT> fuNCtion has many

variations to let you access files in different ways. Table 9.4

shows all of the different methods used to open a file.<BR>

<P>

<CENTER><B>Table 9.4&nbsp;&nbsp;The Different Ways to Open a File</B></CENTER>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD WIDTH=175><I>Open Statement</I></TD><TD WIDTH=415><I>Description</I>

</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>);

</TD><TD WIDTH=415>Opens the file named in <TT>$FILE_HANDLE</TT> and connect to it using <TT>FILE_HANDLE</TT> as the file handle. The file will be opened for input only.

</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>FILENAME.EXT</I></TT>);

</TD><TD WIDTH=415>Opens the file called <TT><I>FILENAME.EXT</I></TT> for input using  <TT>FILE_HANDLE</TT> as the file handle.

</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, +<TT><I>&lt;FILENAME.EXT</I></TT>);

</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for input using <TT>FILE_HANDLE</TT> as the file handle.

</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>&gt;FILENAME.EXT</I></TT>);

</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for output using <TT>FILE_HANDLE</TT> as  the file handle.

</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>-</I></TT>);

</TD><TD WIDTH=415>Opens standard input.</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>&gt;-</I></TT>);

</TD><TD WIDTH=415>Opens standard output.</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>&gt;&gt;FILENAME.EXT</I></TT>);

</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for appending using <TT>FILE_HANDLE</TT> as the file handle.

</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>+&lt;FILENAME.EXT</I></TT>);

</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for both input and output using <TT>FILE_HANDLE</TT> as the file handle.

</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>+&gt;FILENAME.EXT</I></TT>);

</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for both input and output using <TT>FILE_HANDLE</TT> as the file handle.

</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>+&gt;&gt;FILENAME.EXT</I></TT>);

</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for both input and output using <TT>FILE_HANDLE</TT> as the file handle.

</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>| PROGRAM</I></TT>)

</TD><TD WIDTH=415>Sends the output printed to <TT>FILE_HANDLE</TT> to another program.

</TD></TR>

<TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>PROGRAM |</I></TT>)

</TD><TD WIDTH=415>Reads the output from another program using  <TT>FILE_HANDLE</TT>.

</TD></TR>

</TABLE>

</CENTER>

<P>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Note</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

I am currently researching the differeNCes between +&lt;, +&gt;, and +&gt;&gt;. The research should be available by 12/1/97 as a link from </BLOCKQUOTE>

<BLOCKQUOTE>

<TT>http:\\www.mtolive.com\pbe\index.html.</TT>

</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

For information about handling failures while opening files, see

<A HREF="ch13.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch13.htm" >Chapter 13</A>, &quot;Handling Errors and Signals.&quot; 

<P>

By prefixing the file name with a &gt; character you open the

file for output. This next example opens a file that will hold

a log of messages.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 writing with </I><TT><I>LOGFILE</I></TT><I>

as the file handle. If the open was successful, a true value will

be returned and the statement block will be executed.<BR>

Send the first message to the </I><TT><I>MESSAGE.LOG</I></TT><I>

file using the </I><TT><I>print()</I></TT><I>

fuNCtion. Notice that an alternate method is being used to call

</I><TT><I>print()</I></TT><I>.<BR>

Send the second 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;message.log&quot;)) {

    print LOGFILE (&quot;This is message number 1.\n&quot;);

    print LOGFILE (&quot;This is message number 2.\n&quot;);

    close(LOGFILE);

}

</PRE>

</BLOCKQUOTE>

<P>

This program displays nothing. Instead, the output from the <TT>print()</TT>

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 second

parameter as a list of things to print. You can find more information

about 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 file

name prefix when opening the file. For example:

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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> is

viewed, 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 what

binary mode is. There are two major differeNCes between binary

mode and text mode:

<UL>

<LI>In DOS and Windows, line endings are indicated by two characters-the

newline and carriage return characters. When in text mode, these

characters are input as a single character, the newline character.

In binary mode, both characters can be read by your program. UNIX

systems only use one character, the newline, to indicate line

endings.

<LI>In DOS and Windows, the end of file character is 26. When

a byte with this value is read in text mode, the file is considered

ended and your program cannot read any more information from the

file. UNIX considers the end-of-file character to be 4. For both

operating systems, binary mode will let the end-of-file character

be 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 file

called BINARY.DAT with the following contents:

<BLOCKQUOTE>

<PRE>

01

02

03

</PRE>

</BLOCKQUOTE>

<P>

First, we'll read the file in the default text mode.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 fuNCtion

call 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 newline

character.</I>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 9.8&nbsp;&nbsp;09LST08.PL-Reading a File to Show Text

Mode 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 0a

30 32 0a

30 33 0a

</PRE>

</BLOCKQUOTE>

<P>

This example does a couple of things that haven't been seen yet

in this book. The <TT>Read()</TT>

fuNCtion is used as an alternative to the line-by-line input done

with the diamond operator. It will read a specified number of

bytes 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 string

into pieces and places those pieces into an array. The double

slashes indicate that each character in the string should be an

element of the new array. 

<P>

For more information about the <TT>split()</TT>

fuNCtion, see <A HREF="ch5.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch5.htm" >Chapter 5</A> &quot;FuNCtions,&quot; and <A HREF="ch10.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 hexadecimal

before displaying it. The <I>ordinal</I> value of a character

is the value of the ASCII representation of the character. For

example, the ordinal value of '0' is 0x30 or 48.

<P>

The next line, the print statement, forces the output onto a new

line if the current character is a newline character. This was

done simply to make the output display look a little like the

input 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 output

is changed.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 newline

character.</I>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 9.9&nbsp;&nbsp;09LST09.PL-Reading a File to Show Binary

Mode 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>

⌨️ 快捷键说明

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