writing formatted data of unknown length to a string (C programming)

后端 未结 5 1047
Happy的楠姐
Happy的楠姐 2020-12-31 11:36

The following C function:

int sprintf ( char * str, const char * format, ... );

writes formatted data to a string. The size of the array pa

5条回答
  •  鱼传尺愫
    2020-12-31 12:04

    What you want is one of these two functions: * snprintf (http://libslack.org/manpages/snprintf.3.html). It takes the length of the output buffer as its second argument, and if the buffer is too small for the result it will return the number of characters needed, allowing you to reallocate a larger buffer. * asprintf. It takes a char ** argument and allocates enough memory to hold the output, as long as that much contiguous virtual memory is available. You have to call free to remove it from memory if you're done with it before the program exits and may need the memory for something else.

提交回复
热议问题