c++ - 奇怪的行为 printf( ) 每行调用 10-13 次

标签 c++ c printf

我编写了这个函数,它使用 printf()

将 2D vector 写入 stdio
static void printSheet ( const storage::twoDVec& Sheet, char format='g'
                       , char elDelim='\t', char vecDelim='\n') 

    const size_t vecSize = Sheet.vecSize;
    const size_t subVecSize = Sheet.subVecSize;
    char pFormat[3] = {'%', format, elDelim};
    for ( size_t y = 0; y < vecSize; y++ ) {
        for ( size_t x = 0; x < subVecSize; x++ ) {
            printf(pFormat, Sheet.get( y, x ));
        }
    putchar(vecDelim);
    }
}

除非 subVecSize 在 [10, 13] 中,否则格式不是很好

x x x
x x x
x x x

对于 subVecSize = 10

1
2
...
9
10

1
2
...
9
10

(重复)。对于 1112,子 vector 沿对角线滚动,对于 13,子 vector 会覆盖自身。 例如 如果 vecSize==1 && subVecSize==3 那么我只会得到一个数字作为输出。

我是不是看错了一些东西,还是我不了解 printf() 的一些基本知识?将 elDelim 更改为 ' ' 不会更改它。

最佳答案

您的 pFormat 数组不是以空值结尾的。试试这个

char pFormat[4] = {'%', format, elDelim, '\0'};

关于c++ - 奇怪的行为 printf( ) 每行调用 10-13 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072339/

相关文章:

php - 为什么 sprintf() 不输出任何东西?

matlab - sprintf 使用时返回 1 行字符数组\n

C++ 类重载 : is it possible to make compiler see which one to use based on template?

c++ - 在 FastCGI 和 Octave 中重新定义标准输出

C 程序 : Do-while loop to run program again not working properly

c - X 窗口 id 0 是什么意思 - 未创建窗口?

c++ - 模板化虚函数

c++ - 如何在单独的文件中使用 `yy_scan_string(const char *str)`(由 lex yacc 生成)

C++ 错误 1 ​​错误 C2227 : left of '->keyPress' must point to class/struct/union/generic type

c - 如何用c语言绘制动态表格