Re: Something like string-streams existing in "C"?

Liste des GroupesRevenir à cl c  
Sujet : Re: Something like string-streams existing in "C"?
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.c
Date : 19. Dec 2024, 03:14:22
Autres entêtes
Organisation : None to speak of
Message-ID : <87a5csxvup.fsf@nosuchdomain.example.com>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13)
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
Inspecting some of my "C" source code here I noticed a construct that
I dislike...
>
    static char buf[32];   // sufficient space for ansi sequence
    ...
    sprintf (buf, "\033[38;5;%dm%c\033[0m", ...);
>
In case of known or deducible string sizes I'm preferring and using
some of the alloc() functions. In the sample I can at least deduce an
upper-bound for the buffer-size. But it's anyway still an undesired
hard-coded buffer size that I'd like to avoid.
>
I recall that in C++ I used "String-Streams" for dynamically extended
strings. But in "C" my reflex (and habit) is to write things like the
above code.
>
Is there something in "C" that allows dynamic flexibility of strings?
(Or are there other options that an experienced "C" programmer would
use instead?)
>
(If there's something available only with newer C-standards I'd also
appreciate a hint.)

You can use snprintf() for this.  If the size argument is too small, it
still returns the number of characters (excluding the terminating '\0')
that would have been written to the buffer.

    char *buf;
    int arg1 = 42;
    int arg2 = 'x';
    int size = snprintf(buf, 0, "\033[38;5;%dm%c\033[0m", arg1, arg2);
    printf("Allocating %d bytes\n", size + 1);
    buf = malloc(size + 1); // error checking skipped
    int actual_size = snprintf(buf, size + 1, "\033[38;5;%dm%c\033[0m", arg1, arg2);
    printf("actual_size = %d, buf = \"%s\"\n", actual_size, buf);

Note that the output includes non-printing characters, so you'll want to
pipe it throught `cat -A` or something similar.

Beware that the size argument includes the terminating null byte, but
the value returned does not (it returns the length of the resulting
string, not its size).

GNU and BSD provide a non-standard asprintf() function that lets you do
this in one step.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */

Date Sujet#  Auteur
19 Dec 24 * Something like string-streams existing in "C"?15Janis Papanagnou
19 Dec 24 +- Re: Something like string-streams existing in "C"?1Keith Thompson
19 Dec 24 +- Re: Something like string-streams existing in "C"?1Lawrence D'Oliveiro
19 Dec 24 +* Re: Something like string-streams existing in "C"?11BlueManedHawk
19 Dec 24 i`* Re: Something like string-streams existing in "C"?10Kaz Kylheku
19 Dec 24 i `* Re: Something like string-streams existing in "C"?9Michael S
19 Dec 24 i  `* Re: Something like string-streams existing in "C"?8Kaz Kylheku
20 Dec 24 i   +* Re: Something like string-streams existing in "C"?6Thiago Adams
20 Dec 24 i   i`* Re: Something like string-streams existing in "C"?5Michael S
20 Dec 24 i   i +* Re: Something like string-streams existing in "C"?3Thiago Adams
20 Dec 24 i   i i+- Re: Something like string-streams existing in "C"?1Michael S
20 Dec 24 i   i i`- Re: Something like string-streams existing in "C"?1Lawrence D'Oliveiro
20 Dec 24 i   i `- Re: Something like string-streams existing in "C"?1Keith Thompson
20 Dec 24 i   `- Re: Something like string-streams existing in "C"?1Michael S
20 Dec 24 `- Re: Something like string-streams existing in "C"?1Janis Papanagnou

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal