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

📄 iolib

📁 UNIX版本6的源代码
💻
📖 第 1 页 / 共 2 页
字号:
.ds s \\s8.ds S \\s0.ds * \v'.2m'*\v'-.2m'.tr ~..ds . \s14~\s0.tr _\(ul.de sn.sp.ft I.ne 2...de sN.sp .5.ft R...TLThe Portable C Library (on \s-2UNIX\s0) *.AUM. E. Lesk.AI.MH.SH1. INTRODUCTION.PPThe C language [1] now exists on three operating systems..FS* This document is an abbreviated form of``The Portable C Library'', by M. E. Lesk, describing onlythe UNIX section of the library..FEA set of library routines common to\*sPDP\*S 11 \*sUNIX\*S, Honeywell 6000 \*sGCOS\*S,and \*sIBM\*S 370 \*sOS\*Shas been provided to improve program portability.This memorandum describes the UNIX implementation ofthe portable routines..PPThe programsdefined here were chosen to follow the standard routinesavailable on \*sUNIX\*S, with alterations to improve transferabilityto other computer systems.  It is expected that future C implementationswill try to support the basic library outlined in this document.It provides character stream input and output on multiple files;simple accessing of files by name; and some elementaryformatting and translating routines.The remainder of this memorandum lists the portable andnon-portable library routines and explains some of the programmingaids available..PPThe I/O routines in the C libraryfall into several classes.Files are addressed through intermediate numbers called.Ifile-descriptors.Rwhich are described in section 2.  Several default file-descriptorsare provided by the system; other aspects of the systemenvironment are explained in section 3..PPBasic character-stream input and output involves the reading or writing offiles considered as streams of characters.The C libraryincludes facilities for this, discussedin section 4.Higher-level character stream operations permit translation ofinternal binary representations of numbers to and fromcharacter representations, and formatting or unpacking of characterdata.These operations are performed with the subprograms in section 5.Binary input and output routines permit data transmissionwithout the cost of translation to or from readable \*sASCII\*S character representations.Such data transmissionshould only be directed to files or tapes, and not toprinters or terminals.  As is usual with such routines, theonly simple guarantee that can be made to the programmer seekingportability is that data written by a particular sequence ofbinary writes, if read by the exactly matching sequence of binary reads,will restore the previous contents of memory.Other reads or writes have system-dependent effects.See section 6 for a discussion of binary input and output.Section 7 describes some further routines in the portable library.These include a storage allocator andsome other control and conversion functions..SH2. FILE DESCRIPTORS.PPExcept for the standard input and output files, all files mustbe explicitly opened beforeany I/O is performed on them.When files are opened for writing, they are created if not already present.They must be closed when finished, although the normal.Icexit.Rroutine will take care of that.When opened a disc file or device is associated with a file descriptor, an integerbetween 0 and 9.This file descriptor is used for further I/O to the file..PPInitially you are given three file descriptors by the system: 0, 1, and 2.File 0 is the standard input; it is normally the teletypein time-sharing or input data cards in batch.File 1 is the standard output; it is normally theteletype in time-sharing or the line printer in batch.File 2 is the error file; it is an output file, normally the sameas file 1, except that when file 1 is diverted via a commandline '>' operator, file 2 remains attached to theoriginal destination, usually the terminal.It is used for error message output.These popular \*sUNIX\*S conventions are considered partof the C library specification.By closing 0 or 1, the default input or output may be re-directed;this can also be done on the command line by.ft I>file.ft Pfor output or.ft I<file.ft Pfor input..PPAssociated with the portable libraryare two external integers,named.Icin.Rand.Icout..RThese are respectively the numbers of thestandard input unit and standard output unit.Initially 0 and 1 are used, but you may redefinethem at any time.These cells are used by the routines.Igetchar,putchar,gets,.Rand.Iputs.Rto select their I-O unit number..SH3. THE C ENVIRONMENT.PPThe C language is almost exactly the same on all machines,except for essential machine differences such as wordlength and number of characters per word. On \*sUNIX\*S\*sASCII\*S character code is used.Characters range from \(mi128 to +127 in numeric value,there is sign extension when characters are assigned to integers,and right shifts are arithmetic.The ``first'' character in a word is stored in the right half word..PPMore serious problems of compatibility are caused by the loaderson the different operating systems..PP\*sUNIX\*S permits external names to be inupper and lower case,up to seven characters long.There may be multiple external definitions (uninitialized) ofthe same name..PPThe C alphabet for identifier names includesthe upper and lower case letters, the digits, and the underline.It is not possible for C programs to communicate with\*sFORTRAN\*S programs..SH4. BASIC CHARACTER STREAM ROUTINES.PPThese routines transfer streams of characters in and out ofC programs.  Interpretation of the characters is left tothe user.Facilities for interpreting numerical strings are presented in section5; and routines to transfer binary data to and from files or devicesare discussed in section 6.In the following routine descriptions, the optionalargument.ft Ifd.ft Prepresents a file-descriptor; if not present, it is takento be 0 for input and 1 for output.  When your programstarts, remember that these are associated with the ``standard''input and output files..snCOPEN (filename, type).sN\fICopen\fR initiates activity on a file; if necessary it willcreate the file too.Up to 10 files may be open at one time.When called as described here,.ft Icopen.ft Rreturns a filedescriptor for a character stream file.Values less than zero returned by.Icopen.Rindicate an error trying to open the file.Other calls to.ft Icopen.ft Rare described in sections 6 and 7..sp 3pArguments :.sp 3p.ft IFilename:.ft Pa string representing a file name, according to thelocal operating system conventions.All accept a string of letters and digits as a legal file name,although leading digits are not recommended on \*sGCOS\*S..br.ft IType:.ft Ra character `r', `w', or `a' meaning read, write, or append.Note that the type is a single character, whereas thefile name must be a string..snCGETC ( fd ).sN.ft ICgetc.ft Rreturns the next character from the input unit associated with \fIfd\fR. On end of file \fIcgetc\fR returns `\\0'.To signal end of file from the teletype,type the special symbol appropriate to \*sUNIX\*S:EOT (control-D).snCPUTC (ch , fd ).sN.ft ICputc.ft Rwrites a character onto the given output unit..ft ICputc.ft Rreturns as its value the character written..PPOutput for disk files is bufferedin 512 character units, irrespectiveof newlines;teletype output goes character by character.snCCLOSE (fd).sNActivity on file.ft Ifd.ft Ris terminated andany output buffers are emptied.You usually don't have to call.ft Icclose; cexit.ft Rwilldo it for you on all open files.However, to write some dataon a file and then read it back in, the correct sequence is:.DSfd = copen (``file'', `w');write on fd ...cclose (fd);fd = copen(``file'', `r');read from fd ....DE.snCFLUSH (fn).sNTo get buffer flushing, but retain the ability to write moreon the file, you may call this routine..PPNormally, output intendedfor the teletype is not bufferedand this call is not needed..snCEXIT ([errcode]).sN.ft ICexit.ft Rcloses all files and then terminates execution.If a non-zero argument is given, this is assumed to bean error indication or other returned value to be signalledto the operating system..PP.ICexit.R.Bmust.Rbe called explicitly; a returnfrom the main program is notadequate..snCEOF (fd).sN.ft ICeof.ft Rreturns nonzero when end of file has been reached on input unit \fIfd.\fR.snGETCHAR ().sN.ft IGetchar.ft Ris a special case of \fIcgetc;\fR it readsone character from the standard input unit..ft IGetchar (\|).ft Ris defined as.ft Icgetc (cin);.ft Rit should not have an argument..snPUTCHAR (ch).sN.ft IPutchar (ch).ft Ris the same as.ft Icputc (ch, cout);.ft Rit writes one character on the standard output..snGETS (s).sN.ft IGets.ft Rreads everything up to the next newline into thestring pointed to by \fIs.\fR  If the last character readfrom this input unit was newline, then \fIgets\fR readsthe next line, which on \*sGCOS\*S and \*sIBM\*S corresponds exactlyto a logical record.The terminating newline is replaced by `\\0'.The value of \fIgets\fR is \fIs,\fR or 0 if end of file..snPUTS (s).sNCopies the string \fIs\fR onto the standard output unit. The terminating `\\0' is replaced by a newline character.The value of.Iputs.Ris.Is..R.snUNGETC (ch , fd).sN.ft IUngetc.ft Rpushes back its character argument to the unit.ft Ifd,.ft Rwhich must be open for input.After.ft Iungetc (`a', fd); ungetc (`b', fd);.ft Rthe next two charactersto be read from \fIfd\fR will be `b' and then `a'.Up to 100 characters may be pushed back on each file.This subroutine permits a program to read past the endof its input, and then restore it for the next routine to read.It is impossible to change an external file with.ft Iungetc;.ft Rits purpose is only for internal communications, most particularly.ft Iscanf,.ft Rwhich is described in section 5.Note that.ft Iscanf.ft Ractually requires only one characterof ``unget'' capability; thus it is possible thatfuture implementors may change the specification of the.ft Iungetc.ft Rroutine..SH5. HIGH-LEVEL CHARACTER STREAM ROUTINES.PPThese two routines,.ft Iprintf.ft Rfor output and.ft Iscanf.ft Rfor input, permit simple translation to and from characterrepresentations of numerical quantities.They also allow generation or interpretation of formattedlines..snPRINTF (\|[fd, ] control-string, arg1, arg2, ...).sp .5PRINTF (\|[\(mi1, output-string, ] control-string, arg1, arg2, ...).sN.ft IPrintf.ft Rconverts, formats, and prints its arguments under control of thecontrol string.The control string contains two types of objects: plain characters,which are simply copied to the output stream, and conversionspecifications, each of which causes conversion and printing of thenext successive argument to \fIprintf\fR..spEach conversion specification is introduced by the character `%'.Following the `%', there may be:.in 5\(em an optional minus sign `\(mi' which specifies left adjustment of theconverted argument in the indicated field;.sp 3p\(em an optional digit string specifying a minimum field width; if the convertedargument has fewer characters than the field width it will be paddedon the left (or right, if the leftadjustment indicator has been given) to make up the field width;the padding character is blank normally and zero if the fieldwidth was specified with a leading zero(note that this does not imply an octal field width);.sp 3p\(em an optional period `.' which serves to separate the field widthfrom the next digit string;.sp 3p\(em an optional digit string (the precision) which specifies the maximumnumber of characters to be printed from a string,or the number of digits to be printed to the rightof the decimal point of a floating or double number..sp 3p\(em an optional length modifier \(fml\(fm which indicates that the correspondingdata item is a.Ilong.Rrather than an.Iint..R.sp 3p\(em a character which indicates the type of conversion to be applied..in 0.spThe conversion characters and their meanings are:.in 5.sp 5p.ti -0.2i\fBd\fR	The argument is converted to decimal notation..sp 3p.ti -0.2i\fBo\fR	The argument is converted to octal notation..sp 3p.ti -0.2i\fBx\fR	The argument is converted to hexadecimal notation..sp 3p.ti -0.2i\fBu\fR	The argument is converted to unsigned decimal notation.This is only implemented (or useful) on \*sUNIX\*S..sp 3p.ti -0.2i\fBc\fR	The argument is taken to be a single character..sp 3p.ti -0.2i\fBs\fR	The argument is taken to be a string and characters from thestring are printed until a null character is reached or until thenumber of characters indicated by the precision specificationis exhausted..sp 3p.ti -0.2i\fBe\fR	The argument is taken to be a float or doubleand converted to decimal notation of the form.ft I[\(mi]\|m.nnnnnnE\|[\(mi]\|xx.ft Rwhere the length of the string of \fIn\fP's is specified bythe precision.The default precision is 6 and the maximum is 22..sp 3p.ti -0.2i\fBf\fR	The argument is taken to be a float or double andconverted to decimal notation of theform.ft I[\(mi]mmm.nnnnn.ft Rwhere the length of the string of\fIn\fR's is specified bythe precision.  The default precision is 6 and the maximumis 22.Note that the precision does not determinethe number of significant digits printed in \fBf\fR format..in 0.sp 3pIf no recognizable conversion character appears after the `%', that characteris printed; thus `%' may be printed by use of the string ``%%''..PPAs an example of \fIprintf,\fR the following program fragment.DS.Iint i, j; float x; char \**s;i = 35; j=2; x= 1.732; s = ``ritchie'';printf (``%d %f %s\\n'', i, x, s);printf (``%o, %4d or %\(mi4d%5.5s\\n'', i, j, j, s);.R.DEwould print.DS.I.cs I 2435 1.732000 ritchie043,    2 or 2   ritch.cs I.R.DE.PPIf \fIfd\fR is not specified, output is to unit.Icout..R

⌨️ 快捷键说明

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