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

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

问题描述

我试图写一个函数,做以下的事情:

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';

不过我也不太清楚,如果这样做,虽然工作。我无法测试它是否会工作,因为我遇到这个错误,当我尝试编译我的code:

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=]

所以,我能做些什么来让我的code的工作?

So what can I do to make my code work?

编辑:改变 scanf函数(%A [^ \\ n]的%* C,&安培;输入)== 1 scanf函数( 作为%[^ \\ n]的%* C,&安培;输入)== 1 给我同样的错误。

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

推荐答案

首先,scanf函数的格式字符串不使用常规的前pressions,所以我不觉得有什么接近你想要将工作内容。至于你的错误,根据我的信赖手动,在%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的期望传递给它分配pviously空缓冲区$ P $它与读取输入我们填写。它不会在初始化字符串str的NULL的情况malloc给你,让你尝试的sctring和相应的FreeS不会与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个arbritrary长度的字符串。创建一个大的缓冲区,并禁止那些长于输入。

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天全站免登陆