简体   繁体   English

C-如何将扩展的char序列与函数strcmp()比较?

[英]C - How to compare extended char sequence with function strcmp()?

I need to compare whether string is equals or not to the following extended char sequence: "———" ( ALT + 0151 code repeated three times) that is in text file. 我需要比较字符串是否等于下面的扩展char序列:“ ———”(ALT + 0151代码重复了三遍),它在文本文件中。 How to do it with function strcmp() ? 如何使用strcmp()函数呢?

A piece of the example text file (TSV): 一个示例文本文件(TSV):

Piracicaba Av. 皮拉西卡巴大道 Armando Salles de Oliveira Lado par 13400-005 Centro Piracicaba Tv. 阿曼多·萨勒斯·德·奥利维拉·拉多par 13400-005 Centro Piracicaba TV。 Agostinho Frasson ——— 13400-008 Centro Piracicaba Av. Agostinho Frasson ——— 13400-008 Centro Piracicaba Av。 Armando Salles de Oliveira Lado ímpar 13400-010 Centro Armando Salles de Oliveira Lado伊姆帕13400-010 Centro

When I read the file and print the field is displayed "ùùù" on monitor. 当我读取文件并打印时,该字段在监视器上显示为“ùùù”。

The structure: 结构:

typedef struct {
    char cidade[50];
    char tipoLogradouro[20];
    char logradouro[50];
    char trecho[30];
    char cep[10];
    char bairro[50];
} Endereco;

The test is inside 'switch case' and the program is crashing in this part: 测试在“开关盒”内部,程序在此部分崩溃:

case 3:
      {
          if(strcmp(token, "———") == 0) // Change to "ùùù" and fails too. 
              strcpy(registro[i].trecho, NULL);
          else
              strcpy(registro[i].trecho, token);
          break;
      }

Thanks a lot. 非常感谢。

Often in C, you can only use 7-bit ASCII in a quoted string, so for upper ASCII you need to use the \\x escape sequence with the hexadecimal code of the character. 通常在C语言中,只能在带引号的字符串中使用7位ASCII,因此对于高位ASCII,您需要使用\\ x转义序列和字符的十六进制代码。 So, in your case you can type: "\\x97\\x97\\x97", since 97 is hex for 151 decimal. 因此,在您的情况下,您可以键入:“ \\ x97 \\ x97 \\ x97”,因为97是十六进制,表示151个十进制。

case 3:
{
      if(strcmp(token, "\x97\x97\x97") == 0) 
          strcpy(registro[i].trecho, NULL);
      else
          strcpy(registro[i].trecho, token);
      break;
}

strcmp only fails on null, you can pretty much just do strcmp仅在null上失败,您几乎可以做

if (strcmp(inputString,"———")==0){
   printf("Strings Equal\n")
} else{
   printf("Strings unequal")
}

If you're trying to just see if the string is in the larger string, strstr is the function your looking for not strcmp. 如果您只是想查看字符串是否在较大的字符串中,则strstr是您要查找的函数,而不是strcmp。

strcpy is for one thing and one thing only , and that is copying one string to another. strcpy是一两件事, 只有一两件事,那就是复制一个到另一个字符串。 If you give it NULL , that's not a string, and dereferencing a NULL pointer is going to cause a crash. 如果给它NULL ,则它不是字符串,并且取消引用NULL指针将导致崩溃。

What you want is this: 您想要的是:

 if (strcmp(token, "———") == 0)
    // Assign NULL pointer
    registro[i].trecho = NULL;
 else
    // Copy string to buffer
    strcpy(registro[i].trecho, token);

Remember strcpy is a very risky function to use as it assumes a lot of things about the destination buffer. 记住strcpy是一个非常冒险的函数,因为它假设了有关目标缓冲区的很多事情。 If trecho isn't large enough to hold the token string, including NULL terminator , you get undefined behaviour. 如果trecho的大小不足以容纳token字符串( 包括NULL终止符) ,则会出现不确定的行为。 If token isn't properly NULL terminated you get undefined behaviour. 如果token没有正确地以NULL终止,则您将获得不确定的行为。 There's a lot of ways this seemingly harmless code can go haywire. 这种看似无害的代码有很多方法可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM