【问题标题】:Can't figure out why this trimming function won't work correctly无法弄清楚为什么此修剪功能无法正常工作
【发布时间】:2011-11-11 15:05:25
【问题描述】:

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

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/'

有什么想法吗?

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

【问题讨论】:

    标签: c gcc gcc4


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!我一直在为此自责。我将其更改为“end - front + 1”并且它有效!当然,我正在检查 end - front + 1 是否小于我对 dest 的最大长度
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      size_t end = sizeof(orig) - 1;
      strncpy(dest, tmp, sizeof(dest) - 1);
      

      你可能在这里想要 strlen 而不是 sizeof。

      【讨论】:

        【解决方案4】:
        void trim(const char * orig, char * dest)
        {
            size_t front = 0;
            size_t end = sizeof(orig) - 1;
        

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

        【讨论】:

        • 我猜 OP 有 64 位操作系统 :)
        【解决方案5】:

        试试这个代码(它不使用临时内存):

        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++;
                }
            }
        }
        

        注意:未经测试!

        【讨论】:

          【解决方案6】:

          您必须在函数中的任何位置将 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
              }
          }
          

          (我已经测试过了)

          【讨论】:

          • 需要 -1 以强制它以 '\0' 空字符结束目标。
          • 我在调试器中看到,字符串 'ABCDEF/G01' 的 sizeof() 返回 7 insted of 10. %)
          猜你喜欢
          • 2014-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-30
          相关资源
          最近更新 更多