我的 C 字符串填充函数不起作用?

发布于 2024-10-06 16:41:11 字数 1009 浏览 2 评论 0原文

我曾尝试自己这样做,但失败了(我很想再次这样做以进行学习,但只需要它作为示例程序)。本质上我希望表示一个二进制数,但当然用 0 填充到最近的字节,所以我在这里找到了另一个问题上的函数:

char * string_pad(char * string, size_t padlen, char * pad) {
    size_t lenstring = strlen(string);
    size_t lenpad = strlen(pad);

    char * padded = (char*)malloc(lenstring + lenpad + 1);
    strncpy(padded, string, lenstring); /* copy without '\0' */
    padded += lenstring; /* prepare for first append of pad */
    for(padlen += 1; padlen > 0; padlen--, padded += lenpad)
        strncpy(padded, pad, lenpad);
    *padded = '\0';
    return padded;
}

我这样调用它:

printf("Test: %s\n", string_pad(dec2bin(~myInt), 32, "0"));

不幸的是它打印“测试: ”但没有别的。顺便说一句,如果您需要知道的话,我的 dec2bin 会返回一个简单的 char 指针。

是什么导致它什么都不做?

为什么这个函数接受 char* pad 而不是 char pad,这样我就可以用“0”填充它,“0”也可以工作吗?或者它是否添加了一个空终止符,将其搞砸了?

编辑:或者有人可以提供一个简单的例子(或者我需要做什么)来填充这个?这个片段似乎不太好..

我正在考虑创建一个初始化为零的字符数组,然后复制二进制文件,但如何让它工作我却忽略了..

I had tried to do it myself but failed (I am tempted to do it again for learning but just need it for an example program). Essentially I wish to represent a binary number but padded of course to the nearest byte with 0's so I found a function on another question here:

char * string_pad(char * string, size_t padlen, char * pad) {
    size_t lenstring = strlen(string);
    size_t lenpad = strlen(pad);

    char * padded = (char*)malloc(lenstring + lenpad + 1);
    strncpy(padded, string, lenstring); /* copy without '\0' */
    padded += lenstring; /* prepare for first append of pad */
    for(padlen += 1; padlen > 0; padlen--, padded += lenpad)
        strncpy(padded, pad, lenpad);
    *padded = '\0';
    return padded;
}

I am calling it like this:

printf("Test: %s\n", string_pad(dec2bin(~myInt), 32, "0"));

Unfortunately it prints "Test: " but nothing else. My dec2bin returns a simple char pointer by the way if you need to know.

What seems to be causing it to do nothing?

Why does this function accept char* pad and not char pad so I can do just pad it with '0', will "0" work too or does it add a null terminator screwing it up or something?

EDIT: Or can somebody provide a simple example (or what I need to do what) to pad left for this? This snippet does not appear to be all that good..

I was thinking of creating a chararray initialized to zero, then copying the binary after that, but how to make it work escaped me..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌面超妹 2024-10-13 16:41:11

当您返回字符串时,“padded”指向字符串的末尾。

"padded" points to the end of the string when you are returning it.

哭泣的笑容 2024-10-13 16:41:11

这是一个简单的示例实现。它依赖于调用者来管理和传递将执行填充的空间。

char *PadLeft(char *bufBeg, size_t bufSize, char padChar)
{
    char *p = bufBeg;
    char *end = bufBeg + bufSize;

    while (p < end && isspace(*p))
    {
         *p = padChar;
         p++;
    }

    return bufBeg;
}

在实际操作中,它看起来像这样...

char padArea[32 + 1];

snprintf(padArea, sizeof(padArea), "%*s", sizeof(padArea) - 1, dec2bin(~myInt));

PadLeft(padArea, sizeof(padArea), '0');
...

...或者...

int padAreaSize = 32 + 1;

char *padArea = malloc(padAreaSize);

snprintf(padArea, padAreaSize, "%*s", padAreaSize - 1, dec2bin(~myInt));

PadLeft(padArea, padAreaSize, '0');

...

free(padArea);

(不要忘记添加错误检查,为清楚起见,此处省略)

pad right 函数将类似:

char *PadRight(char *bufBeg, size_t bufSize, char padChar)
{
    char *p = bufBeg + bufSize;

    while (--p >= bufBeg && isspace(*p))
    {
         *p = padChar;        
    }

    return bufBeg;
}

int padAreaSize = 32 + 1;

char *padArea = malloc(padAreaSize);

snprintf(padArea, padAreaSize, "%-*s", padAreaSize - 1, dec2bin(~myInt));

PadRight(padArea, padAreaSize, '0');

...

free(padArea);

Here is a simple example implementation. It relies on the caller to manage and pass in the space where the padding will be performed.

char *PadLeft(char *bufBeg, size_t bufSize, char padChar)
{
    char *p = bufBeg;
    char *end = bufBeg + bufSize;

    while (p < end && isspace(*p))
    {
         *p = padChar;
         p++;
    }

    return bufBeg;
}

In action it would look like this...

char padArea[32 + 1];

snprintf(padArea, sizeof(padArea), "%*s", sizeof(padArea) - 1, dec2bin(~myInt));

PadLeft(padArea, sizeof(padArea), '0');
...

...or...

int padAreaSize = 32 + 1;

char *padArea = malloc(padAreaSize);

snprintf(padArea, padAreaSize, "%*s", padAreaSize - 1, dec2bin(~myInt));

PadLeft(padArea, padAreaSize, '0');

...

free(padArea);

(don't forget to add error checking, left out here for clarity)

A pad right function would be similar:

char *PadRight(char *bufBeg, size_t bufSize, char padChar)
{
    char *p = bufBeg + bufSize;

    while (--p >= bufBeg && isspace(*p))
    {
         *p = padChar;        
    }

    return bufBeg;
}

int padAreaSize = 32 + 1;

char *padArea = malloc(padAreaSize);

snprintf(padArea, padAreaSize, "%-*s", padAreaSize - 1, dec2bin(~myInt));

PadRight(padArea, padAreaSize, '0');

...

free(padArea);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文