在 C 中获得无限输入?

Getting unlimited input in C?

所以我正在尝试编写一个允许用户输入无限量字符的函数。例如:

char string[100]

将输入限制为 100 个字符。

我目前的代码是:

#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;
}

我是指针的新手,所以我不确定为什么这不起作用(程序崩溃)。我想做的是继续读取一个字符并继续重新定位字符串指针,以便字节数不断增加,直到用户按下 enter ('\n')。

谢谢 ~拉夫

好的,我认为这是问题所在

您正在重新分配

realloc(string, counter * sizeof(char));

第一次迭代中字符串的大小是多少?它将是 0.

现在您正在写入分配了 0 个字节的指针,因此出现段错误。

将其更改为 while loop 可以帮助修复它。您也可以更改计数器的初始值来修复它

方法是合理的,但有一些小细节是错误的。如果您在启用警告的情况下进行编译,您会注意到您缺少 <stdlib.h>;另外,您将 第一个字符 赋予 printf 而不是指向缓冲区的指针。

然后有一个明显的错误,你的大小被重置为 0,你 casting the return value of malloc,使用 char 来存储 getchar() 的结果,这也是错误的,因为你不能检查 EOF。您没有保存 realloced 指针;而且您没有正确终止字符串。在次要细节上,您希望将每个 realloc 中的缓冲区大小加倍,因为 realloc 可能需要复制整行,因此随着行长度的增加,它会随着时间的推移变得越来越慢.

因此我们得到:

#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 - 1] = '[=10=]';
    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;
}

您可以尝试以下操作:

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

struct person{
    char *name;
}pers;

void addMem(void);

int main(void){
    addMem();

    printf("\nYour name is:>  %s\n",pers.name);
    free(pers.name);
    pers.name = NULL;
    return 0;
}

void addMem(void){
    unsigned int length = 6;
    size_t newLength = 0;
    unsigned int newSize = 0;
    unsigned int i =0;
    char *name;
    int c;

    name = malloc(length);
    if(name == NULL){
        exit(1);
    }
    newSize = length;

    printf("Enter your name:>  ");

    while ((c = getchar()) != '\n' && c!=EOF){
        name[i++]=(char)c;

        if(i == newSize){
            newSize = i+length;
            name = realloc(name, newSize);
        }
    }

    name[i] = '[=10=]';

    newLength = strlen(name)+1;
    pers.name = malloc(newLength);

    memcpy(pers.name, name, newLength);

    free(name);
    name = NULL;
}

另一种方法是使用fgets(),它从输入流中获取一个字符串到一定大小的缓冲区中;如果它有完整的输入,那么字符串以 \n 结尾;如果没有,那么它就没有。所以你可以循环调用 fgets 直到最后有一个 EOL 字符,然后根据你的程序对输入的处理,你可以决定是继续重新分配还是一次处理输入。

使用 getcharmallocrealloc 读取无限输入字符串

声明String类型,也可以使用char *

// String type
typedef char *String;

我写这个函数是为了在字符串的末尾加入字符

/**
 * Join the Char into end of String
 *
 * @param string - String
 * @param c - joined char
 */
void String_joinChar(String *string, const char c)
{
  const size_t length = strlen(*string);
  (*string) = (String)realloc((*string), sizeof(char) * (length + 2));
  (*string)[length] = c;
  (*string)[length + 1] = '[=11=]';
}

此函数用于输入字符串,它使用getchar从键盘读取字符并将其加入到当前字符串的末尾。

/**
 * Input String
 *
 * @return Inputed String
 */
String String_input()
{
  String string = (String)malloc(sizeof(char));
  strcpy(string, "");

  char cursor;
  fflush(stdin);
  while ((cursor = getchar()) != '\n' && cursor != EOF)
  {
    String_joinChar(&string, cursor);
  }

  return string;
}

使用 char *mallocrealloc 的原因,我们必须释放它

/**
 * Destroy String
 *
 * @param string - Destroyed String
 */
void String_destroy(String string)
{
  free(string);
}

现在我们就用它!!

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

int main()
{
  String string = String_input();
  printf("\n%s\n", string);
  String_destroy(string);
  return 0;
}

希望对你有用!