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

📄 puman4.n

📁 早期freebsd实现
💻 N
📖 第 1 页 / 共 2 页
字号:
.\" Copyright (c) 1980, 1993.\"	The Regents of the University of California.  All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\"    notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\"    notice, this list of conditions and the following disclaimer in the.\"    documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\"    must display the following acknowledgement:.\"	This product includes software developed by the University of.\"	California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\"    may be used to endorse or promote products derived from this software.\"    without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\"	@(#)puman4.n	8.1 (Berkeley) 6/8/93.\".if !\n(xx \{\.so tmac.p \}.nr H1 3.if n 'ND.NHInput/output.PPThis section describes features of the Pascal input/output environment,with special consideration of the features peculiar to aninteractive implementation..NH 2Introduction.PPOur first sample programs, in section 2, used the file.I output .We gave examples there of redirecting the output to a file and to the lineprinter using the shell.Similarly, we can read the input from a file or another program.Consider the following Pascal program which is similar to the program.I cat (1)..LS% \*bpix -l kat.p <primes\fR.so katout%.LEHere we have used the shell's syntax to redirect the program input froma file in.I primesin which we had placed the output of our prime number program of section 2.6.It is also possible to`pipe' input to this program much as we piped inputto the line printer daemon.I lpr(1)before.Thus, the same output as above would be produced by.LS% \*bcat primes | pix -l kat.p\fR.LE.PPAll of these examples use the shell to control the input and outputfrom files.One very simple way to associate Pascal files with named.UXfiles is to place the file name in the.B programstatement.For example, suppose we have previously created the file.I data.We then use it as input to another version of a listing program..LS% \*bcat data\fR.so data% \*bpix -l copydata.p\fR.so copydataout%.LEBy mentioning the file.I datain the.B programstatement, we have indicated that we wish itto correspond to the.UXfile.I data .Then, when we`reset(data)',the Pascal system opens our file `data' for reading.More sophisticated, but less portable, examples of using.UXfiles will be given in sections 4.5 and 4.6.There is a portability problem even with this simple example.Some Pascal systems attach meaning to the ordering of the file in the.B programstatement file list..UPdoes not do so..NH 2Eof and eoln.PPAn extremely common problem encountered by new users of Pascal, especiallyin the interactive environment offered by.UX ,relates to the definitions of.I eofand.I eoln .These functions are supposed to be defined at the beginning of execution ofa Pascal program, indicating whether the input device is at the end of aline or the end of a file.Setting.I eofor.I eolnactually corresponds to an implicit read in which the input isinspected, but no input is ``used up''.In fact, there is no way the system can know whether the input isat the end-of-file or the end-of-line unless it attempts to read a line from it.If the input is from a previously created file,then this reading can take place without run-time action by the user.However, if the input is from a terminal, then the inputis what the user types.\*(dgIf the system were to do an initial readautomatically at the beginning of program execution,and if the input were a terminal,the user would have to type some input before execution could begin..FS\*(dgIt is not possible to determine whether the input isa terminal, as the input may appear to be a file but actually be a.I pipe,the output of a program which is reading from the terminal..FEThis would make it impossible for the program to begin by promptingfor input or printing a herald..PP.UPhas been designed so that an initial read is not necessary.At any given time, the Pascal system may or may not know whether theend-of-file or end-of-line conditions are true.Thus, internally, these functions can have three values \-true, false, and ``I don't know yet; if you ask me I'll have tofind out''.All files remain in this last, indeterminate state until the Pascalprogram requires a value for.I eofor.I eolneither explicitly or implicitly, e.g. in a call to.I read .The important point to note here is that if you force the Pascalsystem to determine whether the input is at the end-of-file or the end-of-line,it will be necessary for it to attempt to read from the input..PPThus consider the following example code.LS\*bwhile not\fP eof \*bdo\fP \*bbegin\fP    write('number, please? ');    read(i);    writeln('that was a ', i: 2)\*bend\fP.LEAt first glance, this may be appear to be a correct programfor requesting, reading and echoing numbers.Notice, however, that the.B whileloop asks whether.I eofis true.I beforethe request is printed.This will force the Pascal system to decide whether the input is at theend-of-file.The Pascal system will give no messages;it will simply wait for the user to type a line.By producing the desired prompting before testing.I eof,the following code avoids this problem:.LSwrite('number, please ?');\*bwhile not\fP eof \*bdo\fP \*bbegin\fP    read(i);    writeln('that was a ', i:2);    write('number, please ?')\*bend\fP.LEThe user must still type a line before the.B whiletest is completed, but the prompt will ask for it.This example, however, is still not correct.To understand why, it is first necessary to know, as we will discuss below,that there is a blank character at the end of each line in a Pascal textfile.The.I readprocedure, when reading integers or real numbers,is defined so that,if there are only blanks left in the file,it will return a zero value and set the end-of-file condition.If, however, there is a number remaining in the file, the end-of-filecondition will not be set even if it is the last number, as.I readnever reads the blanks after the number, and there is always at leastone blank.Thus the modified code will still put out a spurious.LSthat was a 0.LEat the end of a session with it when the end-of-file is reached.The simplest way to correct the problem in this example is to use the procedure.I readlninstead of.I readhere.In general, unless we test the end-of-file condition both before andafter calls to.I reador.I readln ,there will be inputs for which our program will attemptto read past end-of-file..NH 2More about eoln.PPTo have a good understanding of when.I eolnwill be true it is necessary to know that in any filethere is a special character indicating end-of-line,and that, in effect, the Pascal system always reads one character ahead of the Pascal.I readcommands.\*(dg.FS\*(dgIn Pascal terms,`read(ch)'corresponds to`ch := input^; get(input)'.FEFor instance,in response to `read(ch)',the system sets.I chto the current input character and gets the next input character.If the current input character is the last character of the line,then the next input character from the file is the new-line character,the normal.UXline separator.When the read routine gets the new-line character,it replaces that character by a blank(causing every line to end with a blank)and sets.I eolnto true..I Eolnwill be true as soon as we read the last character of the line and.B beforewe read the blank character corresponding to the end of line.Thus it is almost always a mistake to write a program which deals withinput in the following way:.LSread(ch);\*bif\fP eoln \*bthen\fP    \fIDone with line\fP\*belse\fP    \fINormal processing\fP.LEas this will almost surely have the effect of ignoring the last characterin the line.The `read(ch)' belongs as part of the normal processing..PPGiven this framework, it is not hard to explain the function of a.I readlncall, which is defined as:.LS\*bwhile not\fP eoln \*bdo\fP    get(input);get(input);.LEThis advances the file until the blank corresponding to the end-of-lineis the current input symbol and then discards this blank.The next character available from.I readwill therefore be the first character of the next line,if one exists..NH 2Output buffering.PPA final point about Pascal input-output must be noted here.This concerns the buffering of the file.I output .

⌨️ 快捷键说明

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