什么是获取用户输入C的最简单的方法? [英] What is the simplest way of getting user input in C?

查看:194
本文介绍了什么是获取用户输入C的最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有方法可以得到C.用户输入大量

There seem to be a LOT of ways you can get user input in C.

什么是需要一点code中的最简单的方法?

What is the easiest way that requires little code?

基本上我需要显示此:

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.

(注:我改变比亚的code,检查 isspace为(),而不仅仅是行结束此外,由于@ matejkramny的评论,使用而(1)而不是,而(真) ...等等,只要我们是旁门左道足以编辑Stroustrup的code,我的SUBBED在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\n");
    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 + max;
            name = (char*)realloc(name, max); /* get a new and larger buffer */
            if (name == 0) quit();
        }
        i++;
    }

    printf("The filename is %s\n", 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 /富/跳回到bar.txt ,但你最终misinter preting他们的意见,并覆盖一个名为 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 ++。它被设计为pcisely管理$ P $这些问题 - 与那些强大的,可扩展的技术,但仍然表现良好。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆