printf证明多行输出

时间:2014-01-10 19:33:51

标签: c printf

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

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

如何格式化printf以使“test_passed”字符串在包含第一个字符串的同一行上右对齐?

Open file ........ test_passed.
Write to file .... test_passed.

1 个答案:

答案 0 :(得分:3)

样品

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