C++ 使用fprintf打印两个字符串之间的空格

C++ 使用fprintf打印两个字符串之间的空格,c++,printf,C++,Printf,我想使用fprintf在同一行上打印两个字符串,它们之间有空格。行必须最多为80个字符,因此我希望两个字符串之间的空格数为80减去两个字符串的长度。这就是我到目前为止所做的: fprintf(pFile, "%s Statistics%80s\n", name.project, currentDateTime()); 我的输出是这样的: AnotherProject Statistics

我想使用
fprintf
在同一行上打印两个字符串,它们之间有空格。行必须最多为80个字符,因此我希望两个字符串之间的空格数为80减去两个字符串的长度。这就是我到目前为止所做的:

fprintf(pFile, "%s Statistics%80s\n", name.project, currentDateTime());
我的输出是这样的:

AnotherProject Statistics                                                             2014-03-13.11:50:10

太长了
currentDateTime()
的字符串长度将始终返回20。是否可以在这种情况下使用
fprintf

计算所需的字段宽度,然后使用
%*s

const int fw = 80 - strlen(name.project) - strlen(" Statistics") - strlen(currentDateTime());
fprintf(pFile, "%s Statistics%*s\n", name.project, fw, currentDateTime());

您可以首先使用
sprintf()
生成格式字符串,根据实际数据插入一个变量。