在 C 中读取用户输入的可变长度字符串
我正在尝试读取可变长度的用户输入并执行一些操作(例如在字符串中搜索子字符串)。
问题是我不知道我的字符串有多大(文本很可能有 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
I am trying to read in a variable length user input and perform some operation (like searching for a sub string within a string).
The issue is that I am not aware how large my strings (it is quite possible that the text can be 3000-4000 characters) can be.
I am attaching the sample code which I have tried and the output:
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);
}
and the output is :
enter a string
bhavya
enter a pattern
av
text is bav 3 pattrn is av 2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请永远不要使用不安全的东西,例如
scanf("%s")
或我个人不喜欢的gets()
- 没有防止缓冲区溢出的方法。您可以使用更安全的输入方法,例如:
您可以设置最大大小,它会检测该行是否输入了过多的数据,并刷新该行的其余部分,这样就不会影响您的下一次输入操作。
你可以用类似的东西来测试它:
Please don't ever use unsafe things like
scanf("%s")
or my personal non-favourite,gets()
- there's no way to prevent buffer overflows for things like that.You can use a safer input method such as:
You can then set the maximum size and it will detect if too much data has been entered on the line, flushing the rest of the line as well so it doesn't affect your next input operation.
You can test it with something like:
在实践中,你不应该太费心去精确。给自己一些余地,在堆栈上留出一些内存并对其进行操作。一旦您想进一步传递数据,您可以使用 strdup(buffer) 并将其放在堆上。了解你的极限。 :-)
In practice you shouldn't bother too much to be precise. Give yourself some slack to have some memory on the stack and operate on this. Once you want to pass the data further, you can use
strdup(buffer)
and have it on the heap. Know your limits. :-)不要使用
scanf
或gets
来解决这个问题,因为正如您所说,没有真正的方法可以知道输入的长度。而是使用fgets
并使用stdin
作为最后一个参数。fgets
允许您指定应读取的最大字符数。如果需要,您可以随时返回并阅读更多内容。scanf(%s)
和gets
读取直到找到终止字符,并且很可能超出缓冲区的长度,从而导致一些难以修复的问题。Don't use
scanf
orgets
for that matter because as you say, there is not real way of knowing just how long the input is going to be. Rather usefgets
usingstdin
as the last parameter.fgets
allows you to specify the maximum number of characters that should be read. You can always go back and read more if you need to.scanf(%s)
andgets
read until they find a terminating character and may well exceed the length of your buffer causing some hard to fix problems.您的情况的主要问题是具有未知大小的字符数组。只需在声明时指定数组大小即可。
The main problem in your case is having char arrays of unknown size. Just specify the array size on declaration.