|
|
1.1 ! root 1: .TH SCANF 3S ! 2: .CT 2 file_io data_man ! 3: .SH NAME ! 4: scanf, fscanf, sscanf \(mi formatted input ! 5: .SH SYNOPSIS ! 6: .nf ! 7: .B #include <stdio.h> ! 8: .PP ! 9: .B ! 10: scanf(format [ , pointer ] ... ) ! 11: .B char *format; ! 12: .PP ! 13: .B ! 14: fscanf(stream, format [ , pointer ] ... ) ! 15: .B FILE *stream; ! 16: .B char *format; ! 17: .PP ! 18: .B ! 19: sscanf(s, format [ , pointer ] ... ) ! 20: .B char *s, *format; ! 21: .fi ! 22: .SH DESCRIPTION ! 23: .I Scanf ! 24: reads from the standard input stream ! 25: .IR stdin . ! 26: .I Fscanf ! 27: reads from the named input ! 28: .IR stream . ! 29: .I Sscanf ! 30: reads from the character string ! 31: .IR s . ! 32: Each function reads characters, interprets ! 33: them according to a format, and stores the results in its arguments. ! 34: Each expects as arguments ! 35: a control string ! 36: .I format, ! 37: described below, ! 38: and a set of ! 39: arguments, normally pointers, ! 40: indicating where the converted input should be stored. ! 41: .PP ! 42: The ! 43: control string ! 44: usually contains ! 45: conversion specifications, which are used to direct interpretation ! 46: of input sequences. ! 47: The control string may contain: ! 48: .TP 4 ! 49: 1. ! 50: Blanks, tabs or newlines, ! 51: which match optional white space in the input. ! 52: .TP 4 ! 53: 2. ! 54: An ordinary character (not ! 55: .LR % ) ! 56: which must match ! 57: the next character of the input stream. ! 58: .TP 4 ! 59: 3. ! 60: Conversion specifications, consisting of the ! 61: character ! 62: .BR % , ! 63: an optional assignment suppressing character ! 64: .BR * , ! 65: an optional numerical maximum field width, and a conversion ! 66: character. ! 67: .PP ! 68: A conversion specification directs the conversion of the ! 69: next input field; the result ! 70: is placed in the variable pointed to by the corresponding argument, ! 71: unless assignment suppression was ! 72: indicated by ! 73: .BR * . ! 74: Conversions other than ! 75: .L c ! 76: and ! 77: .L [ ! 78: skip white space and consume ! 79: non-white-space characters up ! 80: to the next inappropriate character or until the field ! 81: width, if specified, is exhausted. ! 82: The field width is either an integer constant or ! 83: .LR ! . ! 84: In the latter case, the width is taken from an integer argument ! 85: that precedes the next pointer argument. ! 86: .PP ! 87: The conversion character indicates the interpretation of the ! 88: input field; the corresponding pointer argument must ! 89: usually be of a restricted type. ! 90: The following conversion characters are legal: ! 91: .TP 4 ! 92: .B % ! 93: A single ! 94: .L % ! 95: is expected ! 96: in the input at this point; ! 97: no assignment is done. ! 98: .TP 4 ! 99: .B d ! 100: A decimal integer is expected; ! 101: the corresponding argument should be an integer pointer. ! 102: .TP 4 ! 103: .B o ! 104: an octal integer is expected; ! 105: the corresponding argument should be an integer pointer. ! 106: .TP 4 ! 107: .B x ! 108: A hexadecimal integer is expected; ! 109: the corresponding argument should be an integer pointer. ! 110: .ti -0.2i ! 111: .TP 4 ! 112: .B s ! 113: A character string is expected; ! 114: the corresponding argument should be a character pointer ! 115: pointing to an array of characters large enough to accept the ! 116: string and a terminating ! 117: .LR \e0 , ! 118: which will be added. ! 119: The input field is terminated by a space character ! 120: or a newline. ! 121: .TP 4 ! 122: .B c ! 123: A character is expected; the ! 124: corresponding argument should be a character pointer. ! 125: If a field width is given, the corresponding argument ! 126: should refer to a character array, and the ! 127: indicated number of characters is read. ! 128: .ne3 ! 129: .TP 4 ! 130: .B e ! 131: .br ! 132: .ns ! 133: .TP ! 134: f ! 135: A ! 136: floating point number is expected; ! 137: the next field is converted accordingly and stored through the ! 138: corresponding argument, which should be a pointer to a ! 139: .IR float . ! 140: The input format for ! 141: floating point numbers is ! 142: an optionally signed ! 143: string of digits ! 144: possibly containing a decimal point, followed by an optional ! 145: exponent field consisting of an ! 146: .B E ! 147: or ! 148: .B e ! 149: followed by an optionally signed integer. ! 150: .ne 3 ! 151: .TP 4 ! 152: .B [ ! 153: .br ! 154: .ns ! 155: .TP ! 156: .B [^ ! 157: A character string is expected. ! 158: The left bracket (or bracket and circumflex) ! 159: is followed by a set of characters and a right ! 160: bracket. ! 161: When the set is introduced by ! 162: .B [ ! 163: (or ! 164: .BR [^ ), ! 165: the string consists only ! 166: of characters in (or not in) the set. ! 167: The corresponding argument must point to a character array. ! 168: .PP ! 169: The conversion characters ! 170: .BR d , ! 171: .B o ! 172: and ! 173: .B x ! 174: may be preceded by ! 175: .B l ! 176: to indicate that a pointer to ! 177: .B long ! 178: rather than to ! 179: .B int ! 180: is in the argument list. ! 181: Similarly, the conversion characters ! 182: .B e ! 183: or ! 184: .B f ! 185: may be preceded by ! 186: .B l ! 187: to indicate a pointer to ! 188: .B double ! 189: rather than to ! 190: .BR float . ! 191: The conversion characters ! 192: .BR d , ! 193: .B o ! 194: and ! 195: .B x ! 196: may be preceded by ! 197: .B h ! 198: to indicate a pointer to ! 199: .BR short . ! 200: .PP ! 201: The ! 202: .I scanf ! 203: functions return the number of successfully matched and assigned input ! 204: items. ! 205: This can be used to decide how many input items were found. ! 206: The constant ! 207: .SM ! 208: .B EOF ! 209: is returned upon end of input; note that this is different ! 210: from ! 211: .LR 0 , ! 212: which means that no conversion was done; ! 213: if conversion was intended, it was frustrated by an ! 214: inappropriate character in the input. ! 215: .PP ! 216: For example, the call ! 217: .nf ! 218: .ft L ! 219: int i; float x; char name[50]; ! 220: scanf("%d%f%s", &i, &x, name); ! 221: .fi ! 222: .ft P ! 223: .PP ! 224: with the input line ! 225: .IP ! 226: .L ! 227: 25 54.32E\(mi1 thompson ! 228: .PP ! 229: will assign to ! 230: .I i ! 231: the value ! 232: .LR 25 , ! 233: .I x ! 234: the value ! 235: .LR 5.432 , ! 236: and ! 237: .I name ! 238: will contain ! 239: .LR thompson\e0 . ! 240: Or, ! 241: .nf ! 242: .ft L ! 243: int i; float x; char name[50]; ! 244: scanf("%2d%f%*d%[1234567890]", &i, &x, name); ! 245: .fi ! 246: .ft P ! 247: .PP ! 248: with input ! 249: .IP ! 250: \fL56789 0123 56a72\fP ! 251: .PP ! 252: will assign ! 253: .L 56 ! 254: to ! 255: .IR i , ! 256: .L 789.0 ! 257: to ! 258: .IR x , ! 259: skip ! 260: .LR 0123 , ! 261: and place the string ! 262: .L 56\e0 ! 263: in ! 264: .IR name . ! 265: The next call to ! 266: .I getchar ! 267: will return ! 268: .LR a . ! 269: .SH "SEE ALSO" ! 270: .IR atof (3), ! 271: .IR stdio (3), ! 272: .IR ungetc (3) ! 273: .SH DIAGNOSTICS ! 274: The ! 275: .I scanf ! 276: functions return ! 277: .B EOF ! 278: on end of input, ! 279: and a short count for missing or illegal data items. ! 280: .SH BUGS ! 281: The success of literal matches and suppressed ! 282: assignments is not directly ! 283: determinable. ! 284: .br ! 285: The input scan stops short of the end of excessively ! 286: long numbers. ! 287: .br ! 288: There is no ! 289: .LR %# . ! 290: .br ! 291: When no maximum field width is given in a ! 292: .L %s ! 293: or ! 294: .L %[] ! 295: conversion specification, improper input can ! 296: overrun the output string and corrupt the program in ! 297: arbitrarily malicious ways. ! 298: The best alternative, ! 299: .LR %!s , ! 300: is nonstandard. ! 301: .br ! 302: A deprecated usage allows upper-case conversion characters ! 303: as equivalents for lower-case characters preceded by ! 304: .LR l .
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.