8

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?

A. Rex
  • 31,633
  • 21
  • 89
  • 96
Thi
  • 2,297
  • 7
  • 26
  • 36

7 Answers7

10

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);
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
9

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());
KeithB
  • 16,577
  • 3
  • 41
  • 45
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.

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • @Ates: it could, but imo not without two calls to `strlen()` if you want to properly check boundaries - therefore, you need to cache this value -> two statements! – Christoph Jan 16 '09 at 19:24
2

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");
    }
Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
2

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 ;)

Christoph
  • 164,997
  • 36
  • 182
  • 240
1

I'd suggest writing a function that pads a string with X characters and use that to generate the first argument to your printf string. Something like:

char s[40];
pad_str(tag, s, 40, '.');
fprintf( stdout, "%-40s[%d]", s, data);

Note that the third line of your sample data would need this format:

"%-40s[%s]"
Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
  • This 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. – sleblanc Nov 19 '19 at 23:23
0

I think there's a better way.

#include <string.h>
#include <stdio.h>

#define MIN(A,B) ((A)<(B)?(A):(B))
char *dashpad(char *buf, int len, const char *instr) {
    memset(buf, '-', len);
    buf[len] = 0;
    int inlen = strlen(instr);
    memcpy(buf, instr, MIN(len, inlen));
    return buf;
}
main() {
    char buf[40];
    printf("%s\n", dashpad(buf, 40, "Hello world, dash padded "));
}
nsayer
  • 16,925
  • 3
  • 33
  • 51