我试图读取可变长度的用户输入并执行一些操作(比如在字符串中搜索子字符串).
问题是我不知道我的字符串有多大(文本很可能是3000-4000个字符).
我附加了我尝试的示例代码和输出:
char t[],p[];
int main(int argc,char** argv) {
fflush(stdin);
printf(" enter a string\n");
scanf("%s",t);
printf(" enter a pattern\n");
scanf("%s",p);
int m=strlen(t);
int n =strlen(p);
printf(" text is %s %d pattrn is %s %d \n",t,m,p,n);
return (EXIT_SUCCESS);
}
输出是:
enter a string bhavya enter a pattern av text is bav 3 pattrn is av 2
解决方法
请不要使用不安全的东西,如scanf(“%s”)或我的个人非喜欢的,gets() – 没有办法防止缓冲区溢出这样的事情.
您可以使用更安全的输入法,例如:
#include <stdio.h>
#include <string.h>
#define OK 0
#define NO_INPUT 1
#define TOO_LONG 2
static int getLine (char *prmpt,char *buff,size_t sz) {
int ch,extra;
// Get line with buffer overrun protection.
if (prmpt != NULL) {
printf ("%s",prmpt);
fflush (stdout);
}
if (fgets (buff,sz,stdin) == NULL)
return NO_INPUT;
// If it was too long,there'll be no newline. In that case,we flush
// to end of line so that excess doesn't affect the next call.
if (buff[strlen(buff)-1] != '\n') {
extra = 0;
while (((ch = getchar()) != '\n') && (ch != EOF))
extra = 1;
return (extra == 1) ? TOO_LONG : OK;
}
// Otherwise remove newline and give string back to caller.
buff[strlen(buff)-1] = '\0';
return OK;
}
然后,您可以设置最大尺寸,它将检测线路上是否输入了太多数据,同时刷新线路的其余部分,这样就不会影响您的下一次输入操作.
您可以使用以下内容进行测试:
// Test program for getLine().
int main (void) {
int rc;
char buff[10];
rc = getLine ("Enter string> ",buff,sizeof(buff));
if (rc == NO_INPUT) {
// Extra NL since my system doesn't output that on EOF.
printf ("\nNo input\n");
return 1;
}
if (rc == TOO_LONG) {
printf ("Input too long [%s]\n",buff);
return 1;
}
printf ("OK [%s]\n",buff);
return 0;
}