Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

skipping spaces in a sprintf format

36 views
Skip to first unread message

Lynn McGuire

unread,
Oct 28, 2022, 6:33:53 PM10/28/22
to
Is there any way to skip spaces in a sprintf format ? In Fortran, one
puts 10x to skip ten spaces.

I am using the following right now:

// WRITE (OUFILE,1700) current_page
// 1700 FORMAT (67X,'PAGE:',I8)
sprintf (termin1.outbuffer, "
PAGE:%8d", prglab1.current_page);
outwri (termin1.outbuffer);

Thanks,
Lynn

Bart

unread,
Oct 28, 2022, 6:48:07 PM10/28/22
to
This prints a string within a field of 10 spaces:

printf("<%*s>\n",10,"");

"*" is used for a variable field width, padded on the left. "%-*s" will
pad on the right.


Manfred

unread,
Oct 28, 2022, 7:45:40 PM10/28/22
to
Simpler, and probably closer to what the OP needs:

sprintf (termin1.outbuffer, "%32s%8d", "PAGE:", prglab1.current_page);
outwri (termin1.outbuffer);


Bart

unread,
Oct 29, 2022, 6:08:48 AM10/29/22
to
I thought I'd missed the obvious solution..

Lynn McGuire

unread,
Oct 29, 2022, 3:02:55 PM10/29/22
to
Thank you both !

Lynn

Lynn McGuire

unread,
Oct 29, 2022, 4:55:37 PM10/29/22
to
On 10/28/2022 6:45 PM, Manfred wrote:
Or,

sprintf (termin1.outbuffer, "%66sPAGE:%8d", " ", prglab1.current_page);
outwri (termin1.outbuffer);

Thanks !

Lynn

Michael S

unread,
Oct 29, 2022, 5:03:59 PM10/29/22
to
Consider bookmarking this page:
https://en.cppreference.com/w/cpp/io/c/fprintf

Also, if were are already discussing C-style formatted output in C++
group, I'd ask about opinion of members of comp/lang.c++ about
boost::format().
I never used it myself, but the idea of combination of std::sstream
class, which I consider the only good part of otherwise awful C++
"native" I/O classes with such obviously well-designed thing as
printf-style formatting looks to me as very promising.

C++20 has something similar (std::format), but for my personal taste
it is too new and unproven. And different from C tradition for no obvious benefit.

Mike Terry

unread,
Oct 29, 2022, 5:30:06 PM10/29/22
to
I'd suggest a minor improvement: ..."%66sPage:%8d", "",...

Looking at:
printf ("==%0s==\n", " ");
printf ("==%0s==\n", "");
We get output
== ==
====

So I could say the empty string works more universally, and SAVES A BYTE OF DISK SPACE!!! :) Well,
probably you would never want to skip 0 spaces, but hey... (it can't be bad to handle edge cases,
even if you're convinced you'll never use them.)


Mike.

Öö Tiib

unread,
Oct 31, 2022, 9:33:29 AM10/31/22
to
My experiments with boost::format were brief.

Its downsides for me were weird % delimited/chained syntax, yet
another kind of format specifiers and performance seemed among
worst of alternatives.

Its upsides were that it was type safe and that it supported positional
specifiers (kind of like printfs that are overridden by gettext support of
positional specifiers).

The compilers are warning about type mismatches in printf arguments
and positional specifiers are only ever used for i18n. I18n implies UTF-8
and support of actual tools for human translators. What tool supports
and integrates with boost::format? Without it ... it is major pain to even
reach agreement with any translation company so those upsides left
me indifferent.

> C++20 has something similar (std::format), but for my personal taste
> it is too new and unproven. And different from C tradition for no obvious benefit.

Agreed. The <format> has all the very same issues that <regex> has. It
mentions supporting UTF-8 exactly zero times, has yet another
"innovative" specifiers no tool supports, has no improvements (like
say those positional specifiers) and so who needs it? For what?
0 new messages