My string padding function in C does not work?

情到浓时终转凉″ 提交于 2019-12-25 02:46:51

问题


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..


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/4355907/my-string-padding-function-in-c-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!