gets()
gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with a null byte ('\0'). No check for buffer overrun is performed (see BUGS below).
--gets()從標(biāo)準(zhǔn)輸入中讀進(jìn)緩沖區(qū)通過s指向知道出現(xiàn)另一新行的終端或者是文件結(jié)束符EOF撒遣,這種情況可能是空值或是'\0'涧窒。不對(duì)緩沖區(qū)做檢查可能造成緩沖區(qū)泛濫的現(xiàn)象(看下面的實(shí)例:BUGS)得糜。
BUGS
Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.
--不要使用gets().因?yàn)榭赡苣闾崆安恢纆ets()將要讀取多少字母讯赏,還有g(shù)ets()會(huì)一直存儲(chǔ)字母即使在緩沖區(qū)結(jié)束以后,這個(gè)用起來(lái)十分危險(xiǎn)茬腿。這已經(jīng)損壞了電腦安全歪赢。請(qǐng)使用fgets()化戳。
It is not advisable to mix calls to input functions from the stdio library with low-level calls to read(2) for the file descriptor associated with the input stream; the results will be undefined and very probably not what you want.
fgets()
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a new‐
line. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.
fgets()最多從流中讀取少于字符串長(zhǎng)度的字母并使用s指向他們存儲(chǔ)在緩沖池中。讀取停止遇到Eof或是新的一行埋凯。如果新的一行被讀迂烁,他被存儲(chǔ)進(jìn)緩沖池。在中斷符號(hào)輸入之后递鹉, ('\0') 會(huì)被自動(dòng)加到最后一個(gè)字母處。
如果文件中的該行藏斩,不足bufsize個(gè)字符躏结,則讀完該行就結(jié)束。如若該行(包括最后一個(gè)換行符)的字符數(shù)超過bufsize-1狰域,則fgets只返回一個(gè)不完整的行媳拴,但是,緩沖區(qū)總是以NULL字符結(jié)尾兆览,對(duì)fgets的下一次調(diào)用會(huì)繼續(xù)讀該行屈溉。
實(shí)際測(cè)試效果
fgets函數(shù)每次都會(huì)把回車鍵讀入,而gets每次使用gcc下都會(huì)編譯出問題抬探!
額外福利:linux下查看當(dāng)前程序的緩沖區(qū)子巾。stdin和stdout,根據(jù)函數(shù)名稱可以知道前者是輸入緩沖區(qū)后者是輸出緩沖區(qū)小压。兩者都是FILE*類型线梗。而在stdio.h文件中可以看到 typedef struct _IO_FILE FILE;其中的_IO_buf_base可以輸出他的緩沖區(qū)。
struct _IO_FILE {
269 int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
270 #define _IO_file_flags _flags
271
272 /* The following pointers correspond to the C++ streambuf protocol. */
273 /* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
274 char* _IO_read_ptr; /* Current read pointer */
275 char* _IO_read_end; /* End of get area. */
276 char* _IO_read_base; /* Start of putback+get area. */
277 char* _IO_write_base; /* Start of put area. */
278 char* _IO_write_ptr; /* Current put pointer. */
279 char* _IO_write_end; /* End of put area. */
280 char* _IO_buf_base; /* Start of reserve area. */
281 char* _IO_buf_end; /* End of reserve area. */
282 /* The following fields are used to support backing up and undo. */
283 char *_IO_save_base; /* Pointer to start of non-current get area. */
284 char *_IO_backup_base; /* Pointer to first valid character of backup area */
285 char *_IO_save_end; /* Pointer to end of non-current get area. */
286
287 struct _IO_marker *_markers;
288
289 struct _IO_FILE *_chain;
291 int _fileno;
292 #if 0
293 int _blksize;
294 #else
295 int _flags2;
296 #endif
297 _IO_off_t _old_offset; /* This used to be _offset but it's too small. */
298
299 #define __HAVE_COLUMN /* temporary */
300 /* 1+column number of pbase(); 0 is unknown. */
301 unsigned short _cur_column;
302 signed char _vtable_offset;
303 char _shortbuf[1];
304
305 /* char* _save_gptr; char* _save_egptr; */
306
307 _IO_lock_t *_lock;
308 #ifdef _IO_USE_OLD_IO_FILE309 };