字符串以换行符结尾的strcmp

时间:2018-05-25 18:21:31

标签: c string newline strcmp

我正在尝试编写一个接收国家/地区字符串的C列表过滤功能,遍历该列表并删除node->country == country的所有节点。问题是node->country字符串以'\n'结尾(因为它是从csv文件中读取的),因此strcmp(node->country, country)永远不会等于零。

我该如何解决这个问题?我首先考虑将'\n'添加到国家/地区,但这可能会引发更多内存问题。还考虑了strstr,但我真的不知道如何使用它。

感谢您的任何建议。

2 个答案:

答案 0 :(得分:2)

在使用strcmp时,让我们假设一些C风格的代码:

node->country[strcspn(node->country, "\n")] = '\0';

这将改变您的node->country值并在新行(如果有)之前终止您的字符串。

答案 1 :(得分:0)

建议的替代方案:

  1. 预计算

    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://www.sample.com/v2/clients?token=DMfJjzWLngIn0JBHA0gWcg",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"active_registration_id\"\r\n\r\n123\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"language_id\"\r\n\r\n79\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"archived\"\r\n\r\n{{archived}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"enabled\"\r\n\r\n{{enabled}}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\nSamplePassword\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password_confirmation\"\r\n\r\nSamplePassword\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[first_name]\"\r\n\r\nJustin\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[last_name]\"\r\n\r\nTrudeau\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[email]\"\r\n\r\nemail@sample.com\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[telephone_1]\"\r\n\r\n555-555-5555\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[date_of_birth]\"\r\n\r\n1943-10-10\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[gender]\"\r\n\r\nMale\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[referral_code]\"\r\n\r\nAmple Clinic 123\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[status]\"\r\n\r\nRegistration Pending\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"For Additional Permitted Parameters:\"\r\n\r\nSee Registration Parameters\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[date_of_birth]\"\r\n\r\n1943-10-10\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[gender]\"\r\n\r\nMale\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[referral_code]\"\r\n\r\nAmple Clinic 123\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"registration_attributes[status]\"\r\n\r\nRegistration Pending\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; ",
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/x-www-form-urlencoded",
        "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }</pre>
    

    并使用

    size_t len_of_country = strlen(country)
    

    作为匹配标准。

  2. 与(1.)相同,但如果是后一种情况,也将(strncmp(country, node->country, len_of_country) == 0 && (node->country[len_of_country] == '\n' || node->country[len_of_country] == '\0')) 设置为0以供日后使用。

  3. 从文件中读取国家/地区字符串时,让CSV解析器选中尾随换行符,然后使用node->country[len_of_country]