How to print out dash or dot using fprintf/printf?
Solution 1
A faster approach:
If the maximum amount of padding that you'll ever need is known in advance (which is normally the case when you're formatting a fixed-width table like the one you have), you can use a static "padder" string and just grab a chunk out of it. This will be faster than calling printf
or cout
in a loop.
static const char padder[] = "......................"; // Many chars
size_t title_len = strlen(title);
size_t pad_amount = sizeof(padder) - 1 - title_len;
printf(title); // Output title
if (pad_amount > 0) {
printf(padder + title_len); // Chop!
}
printf("[%d]", data);
You could even do it in one statement, with some leap of faith:
printf("%s%s[%d]", title, padder + strlen(title), data);
Solution 2
You can easily do this with iostreams instead of printf
cout << setw(40) << setfill('.') << left << tag[i] << '[' << data[i] << ']' << endl;
Or if you really need to use fprintf (say, you are passed a FILE* to write to)
strstream str;
str << setw(40) << setfill('.') << left << tag[i] << '[' << data[i] << ']' << endl;
printf(%s", str.str());
Solution 3
You can't do it in one statement. You can use sprintf, then substitute dots for spaces yourself, or do something like
int chars_so_far;
char padder[40+1]= '..........'; //assume this is 40 dots.
printf("%.40s%n",tag,&chars_so_far);
printf("%s[%d]",padder+chars_so_far,data);
Edit: Simplified my example above based on @Ates' padder concept. This way doesn't require any 'leaps of faith', about whether the tag string is too big or too small - it always starts the data in column 41.
Solution 4
Another solution using a tiny helper function
static inline size_t min(size_t a, size_t b)
{
return a < b ? a : b;
}
Then, you can do the following:
char padder[] = "........................................";
int len = min(strlen(tag), sizeof(padder) - 1);
printf("%.*s%s[%d]", len, tag, padder + len, data);
This is essentially what Ates posted, but I actually figured this out on my own ;)
Solution 5
You are going to have to output the string with the dot or dash padding yourself.
Something like (forgive my C, it's rusty):
printAmount(char *txt, int amt) {
printf("%s",txt);
for(int xa=strlen(txt); xa<40; xa++) { putc('.'); }
printf("[%d]",amt);
printf("\n");
}
Comments
-
Thi over 1 year
As of now I'm using below line to print with out dot's
fprintf( stdout, "%-40s[%d]", tag, data);
I'm expecting the output would be something like following,
Number of cards..................................[500] Fixed prize amount [in whole dollars]............[10] Is this a high winner prize?.....................[yes]
How to print out dash or dot using fprintf/printf?
-
Christoph over 15 yearsyour last suggestion breaks if
strlen(title) > strlen(padder)
-
Ates Goral over 15 years@Christoph, hence the proclaimed "leap of faith" :)
-
Christoph over 15 years@Ates: interesting concept, this 'faithful programming' of yours ;)
-
Jonathan Leffler over 15 yearsChange the size_t to int; you must pass an int to the '*' in the printf() family of functions.
-
Ates Goral over 15 yearsOr make the helper more helpful: printf("%s%s[%d]", title, getPadding(title), data);
-
Yavor Angelov over 15 years+1 for being easy to read and understand and not breaking under certain (albeit somewhat uncommon situations).
-
Ates Goral over 15 yearsFirst filling the entire buffer with the pad character, and then overwriting them seems a bit wasteful though...
-
Lawrence Dol over 15 yearsAlso, you have a buffer overrun (don't you love C) - s/be: buf[len-1]=0;
-
Thi over 15 yearsis it possible to call a function inside printf?
-
Lawrence Dol over 15 years@Thi: Yes this is legitimate - the function is invoked and it's return passed as a parameter to printf... this is normal coding in any procedural, OOP or functional language.
-
vitrums almost 10 yearssize_t -> int is required in
if ((int)pad_amount > 0)
-
sleblanc over 4 yearsThis makes the most sense. If you are padding to a fixed amount of characters, you probably want to chop titles that are too long, so it makes sense to use fixed-width arrays.