|
|
1.1 ! root 1: ! 2: ! 3: gets() STDIO Function gets() ! 4: ! 5: ! 6: ! 7: ! 8: Read string from standard input ! 9: ! 10: #include <stdio.h> ! 11: cchhaarr *ggeettss(_b_u_f_f_e_r) cchhaarr *_b_u_f_f_e_r; ! 12: ! 13: gets reads characters from the standard input into a buffer ! 14: pointed at by _b_u_f_f_e_r. It stops reading as soon as it detects a ! 15: newline character or EOF. gets discards the newline or EOF, ap- ! 16: pends a null character onto the string it has built, and returns ! 17: another copy of _b_u_f_f_e_r. ! 18: ! 19: ***** Example ***** ! 20: ! 21: The following example uses ggeettss to get a string from the console; ! 22: the string is echoed twice to demonstrate what ggeettss returns. ! 23: ! 24: ! 25: #include <stdio.h> ! 26: ! 27: main() ! 28: { ! 29: char buffer[80]; ! 30: ! 31: ! 32: ! 33: printf("Type something: "); ! 34: ! 35: ! 36: ! 37: /* ! 38: * because of the way COHERENT's teletype ! 39: * driver works, the following fflush has ! 40: * no effect. It should be included for ! 41: * portability to other operating systems. ! 42: */ ! 43: ! 44: ! 45: ! 46: fflush(stdout); ! 47: printf("%s\n%s\n", gets(buffer), buffer); ! 48: } ! 49: ! 50: ! 51: ***** See Also ***** ! 52: ! 53: buffer, fgets(), getc(), STDIO ! 54: ! 55: ***** Diagnostics ***** ! 56: ! 57: gets returns NULL if an error occurs or if EOF is seen before any ! 58: characters are read. ! 59: ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: gets() STDIO Function gets() ! 70: ! 71: ! 72: ! 73: ***** Notes ***** ! 74: ! 75: gets stops reading the input string as soon as it detects a ! 76: newline character. If a previous input routine left a newline ! 77: character in the standard input buffer, gets will read it and im- ! 78: mediately stop accepting characters; to the user, it will appear ! 79: as if gets is not working at all. ! 80: ! 81: For example, if getchar is followed by gets, the first character ! 82: ggeettss will receive is the newline character left behind by ! 83: ggeettcchhaarr. A simple statement will remedy this: ! 84: ! 85: ! 86: while (getchar() != '\n') ! 87: ; ! 88: ! 89: ! 90: This throws away the newline character left behind by getchar; ! 91: gets will now work correctly. ! 92: ! 93: ! 94: ! 95: ! 96: ! 97: ! 98: ! 99: ! 100: ! 101: ! 102: ! 103: ! 104: ! 105: ! 106: ! 107: ! 108: ! 109: ! 110: ! 111: ! 112: ! 113: ! 114: ! 115: ! 116: ! 117: ! 118: ! 119: ! 120: ! 121: ! 122: ! 123: ! 124: ! 125: ! 126: ! 127: ! 128: ! 129: ! 130: COHERENT Lexicon Page 2 ! 131: ! 132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.