不明白为什么这个修剪功能不能正常工作

发布于 2024-12-15 00:22:01 字数 1143 浏览 1 评论 0原文

=================================================== ===================================================

void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = sizeof(orig) - 1;
    size_t counter = 0;
    char * tmp = null;

    if (sizeof(orig) > 0)
    {
        memset(dest, '\0', sizeof(dest));

        /* Find the first non-space character */
        while (isspace(orig[front]))
        {
                front++;
        }
        /* Find the last non-space character */
        while (isspace(orig[end]))
        {
                end--;
        }

        tmp = strndup(orig + front, end - front + 1);
        strncpy(dest, tmp, sizeof(dest) - 1);
        free(tmp); //strndup automatically malloc space
    }
}

​=================================================== ========

我有一个字符串:

'    ABCDEF/G01       '

上面的函数应该删除空格并返回给我:

'ABCDEF/G01'

相反,我得到的结果是:

'ABCDEF/'

有什么想法吗?

注意:引号只是为了告诉您原始字符串中存在空格。

===============================================================================

void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = sizeof(orig) - 1;
    size_t counter = 0;
    char * tmp = null;

    if (sizeof(orig) > 0)
    {
        memset(dest, '\0', sizeof(dest));

        /* Find the first non-space character */
        while (isspace(orig[front]))
        {
                front++;
        }
        /* Find the last non-space character */
        while (isspace(orig[end]))
        {
                end--;
        }

        tmp = strndup(orig + front, end - front + 1);
        strncpy(dest, tmp, sizeof(dest) - 1);
        free(tmp); //strndup automatically malloc space
    }
}

===============================================================================

I have a string:

'     ABCDEF/G01        '

The above function is supposed to remove the spaces and return to me:

'ABCDEF/G01'.

Instead, what I get back is:

'ABCDEF/'

Any ideas?

Note: the quotes are just to show you that spaces exist in the original string.

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

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

发布评论

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

评论(6

凉世弥音 2024-12-22 00:22:01

strncpy 是错误的。 sizeof(dest) 不是您想要的(它是您机器上指针的大小)。您可能需要:end - front。相反,请尝试:

memcpy(dest, front + start, end - front);
dest[end] = 0;

The strncpy is wrong. sizeof(dest) is not what you want (it's the size of a pointer on your machine). You probably want: end - front. Instead, try:

memcpy(dest, front + start, end - front);
dest[end] = 0;
眼眸印温柔 2024-12-22 00:22:01

sizeof(dest) 并没有像你想象的那样做!它返回指针的大小,而不是字符串的长度。您需要为您的函数提供目的地的最大长度。

对于字符串 orig,您要使用 strlen 函数。

The sizeof(dest) doesn't do what you think it does! It returns the size of the pointer, not the length of the string. You need to supply a maximum length of the destination to your function.

For the string orig you want to use the strlen function.

橘虞初梦 2024-12-22 00:22:01
size_t end = sizeof(orig) - 1;
strncpy(dest, tmp, sizeof(dest) - 1);

您可能需要 strlen 而不是 sizeof 。

size_t end = sizeof(orig) - 1;
strncpy(dest, tmp, sizeof(dest) - 1);

You probably want strlen instead of sizeof here.

メ斷腸人バ 2024-12-22 00:22:01
void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = sizeof(orig) - 1;

在该代码中,sizeof(orig) 是指针的大小。所有指针的大小都相同,在您的实现中可能是 8。您想使用的是 strlen(orig)

void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = sizeof(orig) - 1;

In that code, sizeof(orig) is the size of a pointer. All pointers are the same size, probably 8 in your implementation. What you want to use is strlen(orig) instead.

眼趣 2024-12-22 00:22:01

尝试此代码(它不使用临时内存):

void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = strlen(orig)-1;
    size_t counter = 0;

    *dest = '\0';

    if (strlen(orig) > 0)
    {    
        /* Find the first non-space character */
        while (front < end && isspace(orig[front]) )
        {
                front++;
        }
        /* Find the last non-space character */
        while (front < end && isspace(orig[end]))
        {
                end--;
        }

        counter = front;
        while ( counter <= end )
        {
                dest[counter-front] = orig[counter];
                counter++;
        }
    }
}

注意:未经测试!

Try this code (it doesn't use temporary memory):

void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = strlen(orig)-1;
    size_t counter = 0;

    *dest = '\0';

    if (strlen(orig) > 0)
    {    
        /* Find the first non-space character */
        while (front < end && isspace(orig[front]) )
        {
                front++;
        }
        /* Find the last non-space character */
        while (front < end && isspace(orig[end]))
        {
                end--;
        }

        counter = front;
        while ( counter <= end )
        {
                dest[counter-front] = orig[counter];
                counter++;
        }
    }
}

NOTE: Not tested!

影子是时光的心 2024-12-22 00:22:01

您必须在函数中的所有位置将 sizeof() 替换为 strlen()。
这是工作编辑:(

void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = strlen(orig)-1;
    size_t counter = 0;
    char * tmp = NULL;

    if (strlen(orig) > 0)
    {
        memset(dest, '\0', strlen(dest));

        /* Find the first non-space character */
        while (isspace(orig[front]))
        {
            front++;
        }
        /* Find the last non-space character */
        while (isspace(orig[end]))
        {
            end--;
        }

        tmp = strndup(orig + front, end - front + 1);
        strncpy(dest, tmp, strlen(dest));
        free(tmp); //strndup automatically malloc space
    }
}

我已经测试过)

You must replace sizeof() to strlen() everywhere in your function.
Here is working edit:

void trim(const char * orig, char * dest)
{
    size_t front = 0;
    size_t end = strlen(orig)-1;
    size_t counter = 0;
    char * tmp = NULL;

    if (strlen(orig) > 0)
    {
        memset(dest, '\0', strlen(dest));

        /* Find the first non-space character */
        while (isspace(orig[front]))
        {
            front++;
        }
        /* Find the last non-space character */
        while (isspace(orig[end]))
        {
            end--;
        }

        tmp = strndup(orig + front, end - front + 1);
        strncpy(dest, tmp, strlen(dest));
        free(tmp); //strndup automatically malloc space
    }
}

(I've tested it)

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