📄 iolib
字号:
It is possible to direct output to a string instead of to a file.This is indicated by \(mi1 as the first argument.The second argument should be a pointer to the string.\fIPrintf\fR will put a terminating `\\0' onto the string..snSCANF (\|[fd, ] control-string, arg1, arg2, ....).sp .5SCANF (\|[\(mi1, input-string, ] control-string, arg1, arg2, ....).sN.ft IScanf.ft Rreads characters, interpretsthem according to a format, and stores the results in its arguments.It expects as arguments:.ti 5.ti -0.2i1. An optional file-descriptor or input-string, indicating the source of the inputcharacters; if omitted, file.Icin.Ris used;.ti -0.2i2. A control string, described below;.ti -0.2i3. A set of arguments,.ft Ieach of which must be a pointer,.ft Rindicating where the converted input should be stored..in 0.sp 5pThecontrol stringusually containsconversion specifications, which are used to direct interpretationof input sequences.The control string may contain:.sp 5p.in 5.ti -0.2i1. Blanks, tabs or newlines, which are ignored..ti -0.2i2. Ordinary characters (not %) which are expected to matchthe next non-space character of the input stream(where space characters are defined as blank, tab or newline)..ti -0.2i3. Conversion specifications, consisting of thecharacter %, an optional assignment suppressing character \**,an optional numerical maximum field width, and a conversioncharacter..in 0.sp 5pA conversion specification is used to direct the conversion of thenext input field; the resultis placed in the variable pointed to by the corresponding argument,unless assignment suppression wasindicated by the \** character.An input field is defined as a string of non-space characters;it extends either to the next space character or until the fieldwidth, if specified, is exhausted..PPThe conversion character indicates the interpretation of theinput field; the corresponding pointer argument mustusually be of a restricted type.Pointers, rather than variable names, are requiredby the ``call-by-value'' semantics of the C language.The following conversion characters are legal:.in 5.ti -0.2i\fB%\fR indicates that a single % character is expectedin the input stream at this point;no assignment is done..tr @ .ti -0.2i\fBd\fR indicates that a decimal integer is expectedin the input stream;the corresponding argument should be an integer pointer..ti -0.2i\fBo\fR indicates that an octal integer is expected in theinput stream; the corresponding argument should be a integer pointer..ti -0.2i\fBx\fR indicates that a hexadecimal integer is expected in the input stream;the corresponding argument should be an integer pointer..ti -0.2i\fBs\fR indicates that a character string is expected;the corresponding argument should be a character pointerpointing to an array of characters large enough to accept thestring and a terminating `\\0', which will be added.The input field is terminated by a space characteror a newline..ti -0.2i\fBc\fR indicates that a single character is expected; thecorresponding argument should be a character pointer;the next input character is placed at the indicated spot.The normal skip over space characters is suppressedin this case;to read the next non-space character, try.ft I%1s..ft R.ti -0.2i\fBe\fR or \fBf\fR indicates that a floating point number is expected in the input stream;the next field is converted accordingly and stored through thecorresponding argument, which should be a pointer to a float.The input format for.Ifloats.Risa string of numberspossibly containing a decimal point, followed by an optionalexponent field containing an E or e followed by a possibly signed integer..ti -0.2i\fB[\fR indicates a string not to be delimited by space characters.The left bracket is followed by a set of characters and a rightbracket; the characters between the brackets define a setof characters making up the string.If the first characteris not circumflex (\|^\|), the input fieldis all characters until the first character not in the set betweenthe brackets; if the first characterafter the left bracket is ^, the input field is all charactersuntil the first character which is in the remaining set of charactersbetween the brackets.The corresponding argument must point to a character array..in 0.PPThe conversion characters.Id, o.Rand.Ix.Rmay be preceded by .Il.Rto indicate that a pointer to.Ilong.Rrather than.Iint.Ris expected.Similarly, the conversion characters.Ie.Ror.If.Rmay be preceded by.Il.Rto indicate that a pointer to .Idouble.Rrather than .Ifloat.Ris in the argument list.The character.Ih.Rwill function similarly in the future to indicate.Ishort.Rdata items..PPFor example, the call.in 10.nf.ft Iint i; float x; char name[50];scanf ( ``%d%f%s'', &i, &x, name);.ft R.in 0with the input line.in 10.ft I25 54.32E\(mi1 thompson.ft R.in 0.fiwill assign to.ft Ii.ft Rthe value25,.ft Ix.ft Rthe value 5.432, and.ft Iname.ft Rwill contain.ft I``thompson\\0''..ft ROr,.ft I.nf.in 10int i; float x; char name[50];scanf (``%2d%f%\**d%[1234567890]'', &i, &x, name);.ft R.in 0with input.ft I.in 1056789 0123 56a72.ft R.in 0.fiwill assign 56 to.ft Ii,.ft R789.0 to.ft Ix,.ft Rskip ``0123'',and place the string ``56\\0'' in.ft Iname..ft RThe next call to \fIcgetc\fR will return `a'..PP.ft IScanf.ft Rreturns as its value the number of successfully matched and assigned inputitems.This can be used to decide how many input items were found.On end of file, \(mi1 is returned; note that this is differentfrom 0, which means that the next input character does notmatch what you called for in the control string..ft IScanf,.ft Rif given a first argument of \(mi1, will scan a string in memorygiven as the second argument. For example,if you want to read up to four numbers from an input lineand find out how many there were, you could try.ft I.in 5.nf.ne 3int a[4], amax;char line[100];amax = scanf (\(mi1, gets(line), ``%d%d%d%d'', &a[0], &a[1], &a[2], &a[3]);.in 0.ft R.fi.SH6. BINARY STREAM ROUTINES.PPThese routines write binary data, not translated to printable characters.They are normally efficientbut do not produce files that can be printed or easily interpreted.No special information is addedto the records and thus they can be handled byother programming systems.ft Iif.ft Ryou make the departure from portability requiredto tell the other system how big a C item (integer, float,structure, etc.) really is in machine units..snCOPEN (name, direction, ``i'').sNWhen.ft Icopen.ft Ris called with a third argument as above, a binary streamfiledescriptor is returned.Such a file descriptor is required for the remainingsubroutines in this section, and may not be used withthe routines in the preceding two sections.The first two arguments operate exactly as described in section3;further details are given in section 7.An ordinary file descriptor may be used for binary I-O,but binary and character I-O may not be mixedunless.Icflush.Ris called at each switch to binary I-O.The thirdargument to.Icopen.Ris ignored..snCWRITE (\|ptr, sizeof(\**ptr), nitems, fd\|).sN.ft ICwrite.ft Rwrites.ft Initems.ft Rof data beginning at.ft Iptr.ft Ron file.ft Ifd.Cwrite.ft Rwrites blocks of binary information, not translated toprintable form, on a file. It is intended for machine-orientedbulk storage of intermediate data. Any kind of data maybe written with this command, but only the corresponding.ft Icread.ft Rshould be expected to make any sense of it on return.The first argument is a pointer to the beginning of a vectorof any kind of data. The second argumenttells.ft Icwrite.ft Rhow big the items are.The third argument specifies the numberof the items to be written; the fourth indicates where..snCREAD (\|ptr, sizeof(\**ptr), nitems, fd\|).sN.ft ICread.ft Rreads up to.ft Initems.ft Rof data from file.ft Ifd.ft Rinto a buffer beginning at.ft Iptr.Cread.ft Rreturns the number of items read..br.ne 2iThe returnednumber of items will be equal to the number requested by.Initems.Rexcept for reading certain devices (e.g. the teletype or magnetic tape)or reading the final bytes of a disk file..LPAgain, the second argument indicates the sizeof the data items being read..snCCLOSE (fd).sNThe same description applies as forcharacter-stream files..SH7. OTHER PORTABLE ROUTINES.LP.snREW (fd).sNRewinds unit.Ifd..RBuffers are emptied properly and the fileis left open..snSYSTEM (string).sNThe given.Istring.Ris executed as if it were typed at the terminal..snNARGS (\|).sNA subroutine can callthis function to try to find out how many arguments it was called with.Normally,.Inargs().Rreturns the number of argumentsplus 3 for every.Ifloat.Ror.Idouble.Rargumentand plus one for every.Ilong.Rargument.If the new \*sUNIX\*S feature of separated instruction and data spaceareas is used,.Inargs().Rdoesn't work at all..snCALLOC (n, sizeof(object)).sN.ICalloc.Rreturns a pointer to new storage, allocated in spaceobtained from the operating system.The space obtained iswell enough aligned for any use, i.e.for a double-precision number.Enough space to store.In.Robjects of the sizeindicated by the second argumentis provided.The.Isizeof.Ris executed at compile time; it is not in the library.A returned value of \(mi1 indicates failure to obtain space..snCFREE (ptr, n, sizeof(*ptr)).sN.ICfree.Rreturns to the operating systemmemory starting at.Iptr.Rand extending for.In.Runits of the size given by the third argument.The space should have been obtained through \fIcalloc\fR.On \*sUNIX\*S you canonly return the exact amount of spaceobtained by .Icalloc;.Rthe second and third arguments are ignored..snFTOA (floating-number, char-string, precision, format\|).sN.ft IFtoa.ft R(floating to \*sASCII\*S conversion)converts floating point numbers to character strings.The.ft Iformat.ft Rargument should be either `f' or `e'; `e' is default.See the explanation of.ft Iprintf.ft Rin section 5 for a description of the result..snATOF (char-string).sNReturns a floating value equal to thevalue of the \*sASCII\*S character string argument,interpreted as a decimal floating point number..snTMPNAM (str).sNThis routine places in the character array expected as its argumenta stringwhich is legal to use as a file nameand which is guaranteed to be uniqueamong all jobs executing on the computer at the sametime. It is thus appropriate for use as a temporaryfile name, althoughthe user may wish to move it into an appropriate directory.The value of the function is the addressof the string..snABORT (code).sNCauses your program to terminate abnormally, which typicallyresults in a dump by the operating system..snINTSS ().sNThis routine tells you whether you are running inforeground or background.The definition of ``foreground'' is thatthe standard input is the terminal..snWDLENG (\|).sNThis returns 16 on \*sUNIX\*S.C users should be aware that the preprocessornormally provides a defined symbol suitable for distinguishing the local system;thus on.SMUNIX.NLthe symbol.Iunix.Ris definedbefore starting to compile your program.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -