【问题标题】:copy a sub part of a string复制字符串的子部分
【发布时间】:2018-05-23 15:31:03
【问题描述】:

我正在尝试将一个字符串的一部分复制到另一个字符串中。 我知道开始和结束子字符串的 2 个标识符。

我正在尝试从该字符串中复制 IP:

0x200085f6 <memp_memory+4502> "GET / HTTP/1.1\r\nHost: 192.168.1.200\r\nConnection

字符串的开头将是“主机:”或 192 结尾将是“\r\nC”或“\r\n”的第二次出现 所以想要的输出是:

192.168.1.200

我尝试使用 strcpy 和 memcpy,但 IP 必须是可变的,所以我不知道它会持续多长时间或它将是什么,最小为 11 个字符,最大为 15 个字符。

希望您能进一步帮助我。

【问题讨论】:

  • 欢迎,请编辑您的问题并添加您的代码...
  • 使用正则表达式会遇到两个问题。

标签: c string memcpy strcpy


【解决方案1】:

您将需要一个 16 字节的缓冲区(每个八位字节最多 3 个字节,总共 12,加上三个点是 15,加上零终止符)。

然后,正如您所说,您需要精确定位您的阅读:

host = strstr(string, "Host: ");
if (!host) {
    // Problem. Cannot continue, the string has not the format expected.
}
host += strlen("Host: ");
end  = strstr(host, "\r\n");
if (!end) {
    // Problem
}
// Sanity check: end - host must be less than 16. Someone could send you
// Host: 192.168.25.12.Hello.I.Am.Little.Bobby.Headers\r\n
if ((end - host) > 15) {
    // Big problem
}
// We now know where the address starts, and where it ends.
// Copy the string into address
strncpy(address, host, (end-host));
// Add zero terminator to make this a C string
address[end-host] = 0x0;

【讨论】:

    【解决方案2】:

    如果输入字符串的格式已知,并且如发布的示例输入所建议的那样,sscanf() 可以与 scanset 指令一起使用以提取 IP 地址。

    使用%*[^:] 会导致sscanf() 匹配输入中的任何字符,直到遇到冒号。 * 禁止分配。使用: 字符继续扫描,因此必须将文字: 放在格式字符串中以匹配该字符。然后%s 指令可用于匹配IP 地址并将其存储为字符串。这是一个例子:

    #include <stdio.h>
    
    #define BUF_SZ  256
    
    int main(void)
    {
        const char *input = "GET / HTTP/1.1\r\nHost: 192.168.1.200\r\nConnection";
    
        char ip_address[BUF_SZ];
        if (sscanf(input, "%*[^:]: %255s", ip_address) == 1) {
            puts(ip_address);
        }
    
        return 0;
    }
    

    程序输出:

    192.168.1.200
    

    【讨论】:

      【解决方案3】:

      一种解决方案是从

      "GET / HTTP/1.1\r\nHost: 192.168.1.200\r\nConnection" till `\r` is encounter.
      

      Host:之后开始提取 记得用 '\0' 结束字符串。

      程序的简化版:

      #include<stdio.h>
      #include<string.h>
      
      int main() {
      
        char ip[64];
        char *p = NULL;
        char *str = "GET / HTTP/1.1\r\nHost: 192.168.1.200\r\nConnection";
        char c;
        int i = 0;
        int len;
      
        len = strlen("Host ");
        p = strstr(str,"Host: ");
      
        if(p)
        {    
            while(1)
            {
              c = *(p+i+len);
      
              if(c != '\r' && c!= '\0')
              {
                  if( c == ' '){ 
                  i++;
                  continue;
                  }
      
                  ip[i++] = c;
              }
              else
              {
                  ip[i] = '\0';
                  break;
              }
      
           } // while
      
        }
      
      
         printf("Ip=%s", ip);
         return 0;
      }
      

      输出:

      Ip=192.168.1.200
      

      【讨论】:

      • 要清楚,'\0' aka NUL 字节是终止符。不要附加字符'0'
      • 问题是这只是字符串的一部分。它包含更多的数字和字母。
      猜你喜欢
      • 2011-01-08
      • 2016-10-03
      • 1970-01-01
      • 2017-10-18
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      相关资源
      最近更新 更多