1
char *func1 = "Open file";
char *func2 = "Write to file";

printf("%s", func1);
open_file();
printf("...test_passed.\n");

How can I format the printf so that the "test_passed" string is right-aligned on the same line that contains the first string?

Open file ........ test_passed.
Write to file .... test_passed.
Korizon
  • 3,677
  • 7
  • 37
  • 52
  • @H2CO3 in my case, a line is being printed using 2 printf statements and not one. so i cant use the `printf(%-20s)` format – Korizon Jan 10 '14 at 19:43
  • This is not a duplicate of the proposed original because the original seeks padding with spaces, which can be done directly with `printf` formatting, but this question seeks padding with periods, which cannot. – Eric Postpischil Jan 10 '14 at 19:47
  • 3
    Many solutions (in C and C++) here http://stackoverflow.com/questions/451475/how-to-print-out-dash-or-dot-using-fprintf-printf – nos Jan 10 '14 at 19:50

1 Answers1

3

sample

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

#define MES_WIDTH 20

int main(){
    char *func1 = "Open file";
    char *func2 = "Write to file";
    char *pass  = "test_passed.";
    char padding[MES_WIDTH + 1] = {0};
    memset(padding, '.', MES_WIDTH);
    printf("%s %.*s %s\n", func1, MES_WIDTH-strlen(func1), padding, pass);
    printf("%s %.*s %s\n", func2, MES_WIDTH-strlen(func2), padding, pass);
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70