动态分配用户输入的字符串 [英] Dynamically allocate user inputted string

查看:25
本文介绍了动态分配用户输入的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个执行以下操作的函数:

I am trying to write a function that does the following things:

  • 开始一个输入循环,打印'>' 每次迭代.
  • 获取用户输入的任何内容(未知长度)并将其读入字符数组,必要时动态分配数组的大小.用户输入的行将以换行符结束.
  • 在字符数组的末尾添加一个空字节 '\0'.
  • 当用户输入空行时循环终止:'\n'
  • Start an input loop, printing '> ' each iteration.
  • Take whatever the user enters (unknown length) and read it into a character array, dynamically allocating the size of the array if necessary. The user-entered line will end at a newline character.
  • Add a null byte, '\0', to the end of the character array.
  • Loop terminates when the user enters a blank line: '\n'

这是我目前写的:

void input_loop(){
    char *str = NULL;

    printf("> ");

    while(printf("> ") && scanf("%a[^\n]%*c",&input) == 1){

        /*Add null byte to the end of str*/

        /*Do stuff to input, including traversing until the null byte is reached*/

        free(str);
        str = NULL;
    }
    free(str);
    str = NULL;
}

现在,我不太确定如何将空字节添加到字符串的末尾.我在想这样的事情:

Now, I'm not too sure how to go about adding the null byte to the end of the string. I was thinking something like this:

last_index = strlen(str);
str[last_index] = '\0';

但我不太确定这是否可行.我无法测试它是否有效,因为我在尝试编译代码时遇到此错误:

But I'm not too sure if that would work though. I can't test if it would work because I'm encountering this error when I try to compile my code:

warning: ISO C does not support the 'a' scanf flag [-Wformat=]

那么我该怎么做才能使我的代码正常工作?

So what can I do to make my code work?

scanf("%a[^\n]%*c",&input) == 1 更改为 scanf("%as[^\n]%*c",&input) == 1 给了我同样的错误.

changing scanf("%a[^\n]%*c",&input) == 1 to scanf("%as[^\n]%*c",&input) == 1 gives me the same error.

推荐答案

首先,scanf 格式的字符串不使用正则表达式,所以我不认为接近你想要的东西会起作用.至于你得到的错误,根据我可靠的手册%a 转换标志用于浮点数,但它仅适用于 C99(并且您的编译器可能配置为 C90)

First of all, scanf format strings do not use regular expressions, so I don't think something close to what you want will work. As for the error you get, according to my trusty manual, the %a conversion flag is for floating point numbers, but it only works on C99 (and your compiler is probably configured for C90)

但是你有一个更大的问题.scanf 期望您将先前分配的空缓冲区传递给它,以便用读取输入填充.它不会为您分配 scstring,因此您尝试将 str 初始化为 NULL 和相应的释放将不适用于 scanf.

But then you have a bigger problem. scanf expects that you pass it a previously allocated empty buffer for it to fill in with the read input. It does not malloc the sctring for you so your attempts at initializing str to NULL and the corresponding frees will not work with scanf.

你能做的最简单的事情就是放弃 n 个任意长度的字符串.创建一个大缓冲区并禁止比这更长的输入.

The simplest thing you can do is to give up on n arbritrary length strings. Create a large buffer and forbid inputs that are longer than that.

然后您可以使用 fgets 函数来填充您的缓冲区.要检查它是否能够读取整行,请检查您的字符串是否以\n"结尾.

You can then use the fgets function to populate your buffer. To check if it managed to read the full line, check if your string ends with a "\n".

char str[256+1];
while(true){
    printf("> ");
    if(!fgets(str, sizeof str, stdin)){
        //error or end of file
        break;
    }

    size_t len = strlen(str);
    if(len + 1 == sizeof str){
        //user typed something too long
        exit(1);
    }

    printf("user typed %s", str);
}

另一种选择是您可以使用非标准库函数.例如,在 Linux 中有 getline 函数,它使用 malloc 读取整行输入在幕后.

Another alternative is you can use a nonstandard library function. For example, in Linux there is the getline function that reads a full line of input using malloc behind the scenes.

这篇关于动态分配用户输入的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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