Dynamic allocation with scanf()

后端 未结 5 1952
孤街浪徒
孤街浪徒 2021-01-25 06:58

My question is exactly the same as this one. That is, I\'m trying to use scanf() to receive a string of indeterminate length, and I want scanf() to dyn

相关标签:
5条回答
  • 2021-01-25 07:21

    If you want to use scanf you could just allocate a large enough buffer to hold any possible value, say 1024 bytes, then use a maximum field width specifier of 1024.

    The m and a are specific non-standard GNU extensions, so thats why Microsofts compiler does not support them. One could wish that visual studio did.

    Here is an example using scanf to read settings, and just print them back out:

    #include <stdio.h>
    #include <errno.h>
    #include <malloc.h>
    
    int
    main( int argc, char **argv )
    {   // usage ./a.out < settings.conf
    
        char *varname;
        int value, r, run = 1;
    
        varname = malloc( 1024 );
    
        // clear errno
        errno = 0;
    
        while( run )
        {   // match any number of "variable = #number" and do some "processing"
    
            // the 1024 here is the maximum field width specifier.
            r = scanf ( "%1024s = %d", varname, &value );
            if( r == 2 )
            {   // matched both string and number
                printf( " Variable %s is set to %d \n", varname, value );
            } else {
                // it did not, either there was an error in which case errno was
                // set or we are out of variables to match
                if( errno != 0 )
                {   // an error has ocurred.
                    perror("scanf");
                }
                run = 0;
            }
        }
    
        return 0;
    }
    

    Here is an example settings.conf

    cake = 5
    three = 3
    answertolifeuniverseandeverything = 42
    charcoal = -12
    

    You can read more about scanf on the manpages.

    And you can of course use getline(), and after that parse character after character.

    If you would go into a little more what you are trying to achieve you could maybe get an better answer.

    0 讨论(0)
  • 2021-01-25 07:23

    Standard versions of scanf() do not allocate memory for any of the variables it reads into.

    If you've been hoodwinked into using a non-standard extension in some version of scanf(), you've just had your first lesson in how to write portable code - do not use non-standard extensions. You can nuance that to say "Do not use extensions that are not available on all the platforms of interest to you", but realize that the set of platforms may change over time.

    0 讨论(0)
  • 2021-01-25 07:24

    Don't use scanf for reading strings. It probably doesn't even do what you think it does; %s reads only up until the next whitespace.

    0 讨论(0)
  • 2021-01-25 07:27

    Must you absolutely use scanf ? Aren't std::string s; std::cin >> s; or getline( std::cin, s ); an option for you?

    0 讨论(0)
  • 2021-01-25 07:32

    I think, in real world, one need to have some maximum limit on length of user input.

    Then you may read the whole line with something like getline(). See http://www.cplusplus.com/reference/iostream/istream/getline/

    Note that, if you want multiple input from user, you don't need to have separate char arrays for each of them. You can have one big buffer, e.g. char buffer[2048], for using with getline(), and copy the contents to a suitably allocated (and named) variable, e.g. something like char * name = strdup( buffer ).

    0 讨论(0)
提交回复
热议问题