Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #389736
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Something like string-streams existing in "C"? |
| Date | 2024-12-18 18:14 -0800 |
| Organization | None to speak of |
| Message-ID | <87a5csxvup.fsf@nosuchdomain.example.com> (permalink) |
| References | <vjvsvb$2i07u$1@dont-email.me> |
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 */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
Something like string-streams existing in "C"? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-19 02:30 +0100
Re: Something like string-streams existing in "C"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-12-18 18:14 -0800
Re: Something like string-streams existing in "C"? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-12-19 04:41 +0000
Re: Something like string-streams existing in "C"? BlueManedHawk <bluemanedhawk@invalid.invalid> - 2024-12-19 08:32 -0500
Re: Something like string-streams existing in "C"? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-12-19 19:47 +0000
Re: Something like string-streams existing in "C"? Michael S <already5chosen@yahoo.com> - 2024-12-19 22:04 +0200
Re: Something like string-streams existing in "C"? Kaz Kylheku <643-408-1753@kylheku.com> - 2024-12-19 22:06 +0000
Re: Something like string-streams existing in "C"? Thiago Adams <thiago.adams@gmail.com> - 2024-12-19 23:14 -0300
Re: Something like string-streams existing in "C"? Michael S <already5chosen@yahoo.com> - 2024-12-20 13:00 +0200
Re: Something like string-streams existing in "C"? Thiago Adams <thiago.adams@gmail.com> - 2024-12-20 08:32 -0300
Re: Something like string-streams existing in "C"? Michael S <already5chosen@yahoo.com> - 2024-12-20 14:55 +0200
Re: Something like string-streams existing in "C"? Lawrence D'Oliveiro <ldo@nz.invalid> - 2024-12-20 20:39 +0000
Re: Something like string-streams existing in "C"? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2024-12-20 10:45 -0800
Re: Something like string-streams existing in "C"? Michael S <already5chosen@yahoo.com> - 2024-12-20 12:38 +0200
Re: Something like string-streams existing in "C"? scott@slp53.sl.home (Scott Lurndal) - 2024-12-19 13:53 +0000
Re: Something like string-streams existing in "C"? Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2024-12-20 03:56 +0100
csiph-web