📄 puman4.n
字号:
It is extremely inefficient for the Pascal system to send each characterto the user's terminal as the program generates it for output;even less efficient if the output is the input of anotherprogram such as the line printer daemon.I lpr(1).To gain efficiency, the Pascal system ``buffers'' the output characters(i.e. it saves them in memory until the buffer is full and then emitsthe entire buffer in one system interaction.)However, to allow interactive prompting to work as in the example givenabove, this prompt must be printed before the Pascal system waits for aresponse.For this reason, Pascal normally prints all the output which hasbeen generated for the file.I outputwhenever.HP.RS.IP 1)A.I writelnoccurs, or.IP 2)The program reads from the terminal, or.IP 3)The procedure.I messageor.I flushis called..RE.LPThus, in the code sequence.ne 5.LS\*bfor\fP i := 1 to 5 \*bdo begin\fP write(i: 2); \fICompute a lot with no output\fP\*bend;\fPwriteln.LEthe output integers will not print until the.I writelnoccurs.The delay can be somewhat disconcerting, and you should be aware that it will occur.By setting the.B boption to 0 before the.B programstatement by inserting a comment of the form.LS(*$b0*).LEwe can cause.I outputto be completely unbuffered, with a corresponding horrendous degradationin program efficiency.Option control in comments is discussed in section 5..NH 2Files, reset, and rewrite.PPIt is possible to use extended forms of the built-in functions.I resetand.I rewriteto get more general associations of.UXfile names with Pascal file variables.When a file other than.I inputor.I outputis to be read or written, then the reading or writing must be precededby a.I resetor.I rewritecall.In general, if the Pascal file variable has never been used before,there will be no.UXfilename associated with it.As we saw in section 2.9,by mentioning the file in the.B programstatement,we could cause a.UXfile with the same name as the Pascal variable to be associated with it.If we do not mention a file in the.B programstatement and use it for the first time with the statement.LSreset(f).LEor.LSrewrite(f).LEthen the Pascal system will generate a temporary name of the form`tmp.x'for some character `x',and associate this.UXfile name name with the Pascal file.The first such generated name will be `tmp.1'and the names continue by incrementing their last character through the.SM ASCIIset.The advantage of using such temporary files is that they are automatically.I remove dby the Pascal system as soon as they become inaccessible.They are not removed, however, if a runtime error causes terminationwhile they are in scope..PPTo cause a particular.UXpathname to be associated with a Pascal file variablewe can give that name in the.I resetor.I rewritecall, e.g. we could have associated the Pascal file.I datawith the file`primes'in our example in section 3.1 by doing:.LSreset(data, 'primes').LEinstead of a simple.LSreset(data).LEIn this case it is not essential to mention `data'in the program statement, but it is still a good ideabecause is serves as an aid to program documentation.The second parameter to.I resetand.I rewritemay be any string value, including a variable.Thus the names of .UXfiles to be associated with Pascal file variables can be readin at run time.Full details on file name/file variable associations are given insection A.3..NH 2Argc and argv.PPEach.UXprocess receives a variablelength sequence of arguments each of which is a variable lengthcharacter string.The built-in function.I argcand the built-in procedure.I argvcan be used to access and process these arguments.The value of the function.I argcis the number of arguments to the process.By convention,the arguments are treated as an array,and indexed from 0 to.I argc \-1,with the zeroth argument being the name of the program being executed.The rest of thearguments are those passed to the command on the command line.Thus, the command.LS% \*bobj /etc/motd /usr/dict/words hello\fR.LEwill invoke the program in the file.I objwith.I argchaving a value of 4.The zeroth element accessed by.I argvwill be `obj', the first `/etc/motd', etc..PPPascal does not provide variable size arrays, nor does it allowcharacter strings of varying length.For this reason,.I argvis a procedure and has the syntax.LSargv(i, a).LEwhere.I iis an integer and.I ais a string variable.This procedure call assigns the (possibly truncated or blank padded).I i \|'thargument of the current process to the string variable.I a .The file manipulation routines.I resetand.I rewritewill strip trailing blanks from their optional second argumentsso that this blank padding is not a problem in the usual casewhere the arguments are file names..PPWe are now ready to give aBerkeleyPascal program `kat',based on that given in section 3.1 above,which can be used with the same syntax as the.UXsystem program.I cat(1)..LS% \*bcat kat.p\fR.so kat3.p%.LENote that the.I resetcall to the file.I inputhere, which is necessary for a clear program,may be disallowed on other systems.As this program deals mostly with.I argcand.I argvand.UXsystem dependent considerations,portability is of little concern..PPIf this program is in the file `kat.p', then we can do.LS% \*bpi kat.p\fR% \*bmv obj kat\fR% \*bkat primes\fR.so kat2out% \*bkat\fR.so katscript%.LEThus we see that, if it is given arguments, `kat' will,like.I cat,copy each one in turn.If no arguments are given, it copies from the standard input.Thus it will work as it did before, with.LS% \*bkat < primes\fR.LEnow equivalent to.LS% \*bkat primes\fR.LEalthough the mechanisms are quite different in the two cases.Note that if `kat' is given a bad file name, for example:.LS% \*bkat xxxxqqq\fR.so xxxxqqqout%.LEit will give a diagnostic and a post-mortem control flow backtracefor debugging.If we were going to use `kat', we might want to translate itdifferently, e.g.:.LS% \*bpi -pb kat.p\fR% \*bmv obj kat\fR.LEHere we have disabled the post-mortem statistics printing, soas not to get the statistics or the full traceback on error.The.B boption will cause the system to block buffer the input/output so thatthe program will run more efficiently on large files.We could have also specified the.B toption to turn off runtime tests if that was felt to be a speed hindranceto the program.Thus we can try the last examples again:.LS% \*bkat xxxxqqq\fR.so xxxxqqqout2% \*bkat primes\fR.so primes-d%.LE.PPThe interested reader may wish to try writing a program whichaccepts command line arguments like.PIdoes, using.I argcand.I argvto process them.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -