获取无限投入使用C? [英] Getting unlimited input in C?

查看:136
本文介绍了获取无限投入使用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想设定功能,它允许用户输入字符无限量的。例如对于这样的:

So I'm trying to program a function which allows the user to enter an unlimited amount of chars. For example this:

char string[100]

限制输入100个字符。

limits the input to 100 characters.

在code我至今是:

#include<stdio.h>

char* uinput(){
    char *string, *current;
    int counter = 0;
    string = (char *) malloc(10 * sizeof(char));
    do{
        realloc(string, counter * sizeof(char));
        current = string + counter;
        *current = getchar();
        counter++;
    }while(*current != '\n');
    return string;
}

int main(){
    char *s;
    s = uinput();
    printf("\nYou entered: %s", *s);
    return 0;
}

我是新来的球,所以我不知道为什么这不起作用(程序崩溃)。我正在试图做的是保持阅读的性格,并保持搬迁字符串指针,以便字节数保持,直到用户presses增加输入('\\ n')

I'm new to pointers, so I'm not sure why this doesn't work(Program crashes). What I'm trying to do is keep reading a character and keep relocating the string pointer so the amount of bytes keeps increasing until the user presses enter ('\n').

谢谢
〜拉夫

推荐答案

这种方法是理智的,但也有小的细节是错误的。如果你启用了警告编译,你会发现你缺少&LT;&stdlib.h中GT; ;还你给的第一个字符的printf 而不是指针到缓冲区。

The approach is sane, but there are minor details that are wrong. If you compile with warnings enabled, you'd notice that you're missing <stdlib.h>; also you're giving the first character to printf instead of the pointer to the buffer.

再有就是明显的错误,你的尺寸被重置为0,而你的铸造的malloc 的返回值,使用字符存储的getchar()这也是错误的,因为你无法对证 EOF 。你不是拯救的realloc ED指针;而你没有正确终止字符串。在小细节,你会希望缓冲区的每个的realloc ,因为的realloc 需要复制的潜在规模翻番整行,所以它成为随时间慢的线生长在长度

Then there is the obvious bug that your size is reset to 0, and you're casting the return value of malloc, using char to store the result of getchar() which is also wrong because you cannot check against EOF. You're not saving the realloced pointer; and you're not terminating the string properly. On minor detail, you'd want to double the size of buffer in each realloc, because realloc needs to potentially copy the whole line, so it becomes slower and slower over time as the line grows in length.

因此​​,我们得到:

#include <stdio.h>
#include <stdlib.h>

char* uinput() {
    char *string;
    // number of characters in the buffer
    size_t counter = 0;

    // size of allocated buffer
    size_t allocated = 16;

    int c;
    string = malloc(allocated);  // sizeof(char) is 1
    do {
        c = getchar();
        if (c == EOF) {
            break;
        }
        // if our buffer is too small, double the allocation 
        if (counter + 2 <= allocated) {
            size_t new_size = allocated * 2;
            char *new_buffer = realloc(string, new_size);
            if (! new_buffer) {
                // out of memory? try smaller increment
                new_size = allocated + 16;
                new_buffer = realloc(string, new_size);
                if (! new_buffer) {
                    // really out of memory: free old block
                    free(string);
                    return NULL;
                }
            }
            allocated = new_size;
            string = new_buffer;
        }
        // store the character
        string[counter++] = c;
    } while (c != '\n');

    // terminate the buffer properly
    string[counter] = 0;
    return string;
}

int main() {
    char *s = uinput();
    if (!s) {
        // possibly out of memory in uinput
        perror("Error reading input");
        exit(EXIT_FAILURE);
    }
    printf("\nYou entered: %s", s);
    free(s);
    return EXIT_SUCCESS;
}

这篇关于获取无限投入使用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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