c - 使用 printf() 添加不需要的空格/行的格式化输出 - C 编程

标签 c string formatting printf string-formatting

这是我遇到问题的函数。主要是最后的 printf 语句。我对 C 比较陌生,但我觉得这是一个不寻常的结果。非常感激任何的帮助!

void frequencies(char htmlDoc[]) {
    tags(htmlDoc, 0);
    int ndx;
    //  struct tagCounter *pCursor = tagCounters[0];
    int size = 1;
    char *name;
    for (ndx = 0; ndx < tagCntSize; ndx++) {
        name = &(tagCounters[ndx]->tagName[0]);
        while (*(name + 1) != '\0') {
            size++;
            name++;
        }
    name = &(tagCounters[ndx]->tagName[0]);
    char thisName[size];
    int index;
    int thisCount = tagCounters[ndx]->tagCount;
    for (index = 0; index < size; index++) {
        thisName[index] = *name;
        name++;
    }
    printf("%s\t%i\n", thisName, thisCount);
    }
}

这是结果输出:

img
�   1
img
    1
img
    1
img
    1
img
    1
img
    1
Ready

最佳答案

char thisName[size];

在此之后执行

memset(thisName,0,sizeof(thisName));

%s 需要一个以 null 结尾的字符串。或者退出循环后

thisName[size] = '\0';

编辑:

数组大小应足够大以容纳字符串的空终止符。

char thisName[size + 1];

关于c - 使用 printf() 添加不需要的空格/行的格式化输出 - C 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29487768/

相关文章:

c - 如何修复 "HDF5-DIAG: Error unable to open file"?

string - 将一个字符替换为另一个字符,反之亦然

string - 将字符串与从输入读取的字符串进行比较在 Rust 中不匹配

python - 有没有办法在 python 中读取模板 excel 文件,包括背景颜色格式?

vba - 将长日期重新格式化为与 Excel 中使用的语言不同的语言

c - C中的结构查找大于某个变量的元素

c - 退出 do while 循环 C 时出错

php - iOS 和 PHP 使用的时间戳格式有区别吗?

C 从文件中读取特定行

没有编码的python 3字符串参数