永发信息网

gets函数如何执行

答案:4  悬赏:0  手机版
解决时间 2021-02-04 02:06
  • 提问者网友:人生佛魔见
  • 2021-02-03 11:08
gets函数的意义以及如何执行
最佳答案
  • 五星知识达人网友:西风乍起
  • 2021-02-03 12:34
函数名: gets
功 能: 从流中取一字符串
用 法: char *gets(char *string);
程序例:

#include <stdio.h>

int main(void)
{
char string[80];

printf("Input a string:");
gets(string);
printf("The string input was: %s\n",
string);
return 0;
}
全部回答
  • 1楼网友:何以畏孤独
  • 2021-02-03 13:46
//以下摘自MSDN gets, _getws Get a line from the stdin stream. char *gets( char *buffer ); wchar_t *_getws( wchar_t *buffer ); Routine Required Header Compatibility gets <stdio.h> ANSI, Win 95, Win NT _getws <stdio.h> or <wchar.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns its argument if successful. A NULL pointer indicates an error or end-of-file condition. Use ferror or feof to determine which one has occurred. Parameter buffer Storage location for input string Remarks The gets function reads a line from the standard input stream stdin and stores it in buffer. The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line. In contrast, the fgets function retains the newline character. _getws is a wide-character version of gets; its argument and return value are wide-character strings. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _getts gets gets _getws Example #include <stdio.h> void main( void ) { char line[81]; printf( "Input a string: " ); gets( line ); printf( "The line entered was: %s\n", line ); } Output Input a string: Hello! The line entered was: Hello! Stream I/O Routines See Also fgets, fputs, puts
  • 2楼网友:你可爱的野爹
  • 2021-02-03 13:39
是的,如果这是某书上的做法,那么,我只能说它为你们提供了一个错误的榜样。 这样使用gets()已经发生了溢出!这是gets()不检查数据边界的bug造成的。 另外,“字符串不是总是以'\0'作为串的结束符”,答案是肯定的,不然puts()函数就不能在合适的地方停下来了。这里st[15]被gets()函数赋值为'\0'。这里要说明的是st[15],st[16]是存在而不合法的,因为字符串实际上就等同于指针,类似st[16]是实在的地址但是是不应该被引用的。 为什么这里溢出没有产生错误?可能系统分配内存是以一个最小的大小整段整段的分配(这个我只是猜测)。你可以试着输入的字符串变长一点,就可以看到内存读写出错的提示了,这就是溢出的严重后果!我在dos系统下测试字符串长了直接当机。 验证1:st[15]被赋值为'\0' #include"stdio.h" main() { charst[15]; printf("inputstring:"); gets(st); puts(st); printf("%d",st[15]); getch(); } 验证2: #include"stdio.h" main() { charst[15]; printf("inputstring:"); gets(st); puts(st); //printf("%d",st[16]); st[1]='\0'; printf("%s\n",st); printf("%s",st+2); getch(); } 同样的建议:拒绝gets(),这本来就是一个有bug的函数!
  • 3楼网友:有你哪都是故乡
  • 2021-02-03 13:00
//计算字符串中数字,字符,空格,其他的个数 #include <iostream> #include <string> using namespace std; int main() { char s[100]; int space=0,zifu=0,number=0,others=0; cout<<"input a string :"<<endl; gets(s); int i,t=strlen(s); for(i=0;i<=t;i++) { if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z') zifu++; else if(s[i]==' ') space++; else if(s[i]>='0'&&s[i]<='9') number++; else others++; } cout<<zifu<<endl<<space<<endl<<number<<endl<<others; return 0; }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯