在 C 中获取用户输入的最简单方法是什么? [英] What is the simplest way of getting user input in C?
问题描述
在 C 中似乎有很多方法可以获取用户输入.
There seem to be a LOT of ways you can get user input in C.
需要很少代码的最简单方法是什么?
What is the easiest way that requires little code?
基本上我需要显示这个:
Basically I need to display this:
Enter a file name: apple.text
基本上我需要向用户询问文件名.所以我需要一些东西来获取用户将输入的那个词.
Basically I need to ask the user for a file name. So I need something that just gets that one word that the user will be inputting.
推荐答案
最简单的正确"方法可能就是这个,摘自 Bjarne Stroustrup 的论文 将标准 C++ 作为一种新语言学习.
The simplest "correct" way is probably this one, taken from Bjarne Stroustrup's paper Learning Standard C++ As A New Language.
(注意:我更改了 Bjarne 的代码以检查 isspace()
而不是行尾.另外,由于@matejkramny 的评论,使用 while(1)
而不是 while(true)
......只要我们足够异端来编辑 Stroustrup 的代码,我也会用 C89 注释代替 C++ 风格.:-P)
(Note: I changed Bjarne's code to check for isspace()
instead of just end of line. Also, due to @matejkramny's comment, to use while(1)
instead of while(true)
...and so long as we're being heretical enough to edit Stroustrup's code, I've subbed in C89 comments instead of C++ style too. :-P)
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
void quit() /* write error message and quit */
{
fprintf(stderr, "memory exhausted
");
exit(1);
}
int main()
{
int max = 20;
char* name = (char*) malloc(max); /* allocate buffer */
if (name == 0) quit();
printf("Enter a file name: ");
while (1) { /* skip leading whitespace */
int c = getchar();
if (c == EOF) break; /* end of file */
if (!isspace(c)) {
ungetc(c, stdin);
break;
}
}
int i = 0;
while (1) {
int c = getchar();
if (isspace(c) || c == EOF) { /* at end, add terminating zero */
name[i] = 0;
break;
}
name[i] = c;
if (i == max - 1) { /* buffer full */
max += max;
name = (char*) realloc(name, max); /* get a new and larger buffer */
if (name == 0) quit();
}
i++;
}
printf("The filename is %s
", name);
free(filename); /* release memory */
return 0;
}
涵盖:
- 跳过空格直到到达字符输入处
- 动态扩展字符串缓冲区以适应任意大小的字符串
- 无法分配内存的处理情况
是否有更简单但破碎的解决方案,甚至可能运行得更快一点?绝对!!
Are there simpler but broken solutions, which might even run a bit faster? Absolutely!!
如果你使用 scanf 进入一个没有读取大小限制的缓冲区,那么你的输入超过了缓冲区的大小,它会产生安全漏洞和/或崩溃.
If you use scanf into a buffer with no limit on the read size, then your input exceeds the size of the buffer, it will create a security hole and/or crash.
将读取的大小限制为例如文件名的 100 个唯一字符似乎比崩溃更好.但情况可能更糟;例如,如果用户的意思是 (...)/dir/foo/bar.txt
但你最终误解了他们的输入并覆盖了一个名为 bar.t
的文件他们关心.
Limiting the size of the reading to, say, only 100 unique characters of a filename might seem better than crashing. But it can be worse; for instance if the user meant (...)/dir/foo/bar.txt
but you end up misinterpreting their input and overwriting a file called bar.t
which perhaps they cared about.
最好在处理这些问题时尽早养成良好的习惯.我的观点是,如果您的需求证明某些接近金属和类 C"的东西是合理的,那么考虑跳到 C++ 是非常值得的.它旨在精确地管理这些问题 - 使用强大且可扩展但仍然表现良好的技术.
It's best to get into good habits early in dealing with these issues. My opinion is that if your requirements justify something close-to-the-metal and "C-like", it's well worth it to consider the jump to C++. It was designed to manage precisely these concerns--with techniques that are robust and extensible, yet still perform well.
这篇关于在 C 中获取用户输入的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!