strcmp returns false 对于相等的字符序列

strcmp returns false for for equal char sequence

我有一个使用 strcmp 做决定的函数,见下文:

void get_data(file_observational_area * _file_observational_area, int dimensions, int * start_indices, int * end_indices) {
    char buffer[1024];
    FILE *file;
    size_t nread;

    if (NULL != _file_observational_area) {

        cout << "***_file_observational_area \n";

        if (NULL != _file_observational_area->_file) {
            //if the data is in a file 

            cout << "****_file_observational_area->_file \n";
            cout << "***_file_observational_area->_file->local_identifier:" << _file_observational_area->_file->local_identifier << "\n";
            cout << "***_file_observational_area->_file->file_name:" << _file_observational_area->_file->file_name << "\n";
            cout << "***data_file_local_identifier:" << data_file_local_identifier << "\n";

            if (strcmp(_file_observational_area->_file->local_identifier, data_file_local_identifier) == 1) {

                if (NULL != _file_observational_area->_file->file_name) {
                    //open the file 
                    file = fopen(_file_observational_area->_file->file_name, "r");
                    if (file) {
                        while ((nread = fread(buffer, 1, sizeof (buffer), file)) > 0)
                            cout << buffer << "\n";
                        if (ferror(file)) {
                            /* deal with error */
                        }
                    }
                }
            } else {
                cout << "***_file_observational_area  NOT A file \n";
            }

            if (NULL != file) {
                fclose(file);
                file = NULL;
            }
        }
    }

}

用于比较的data_file_local_identifier定义在struct:

#ifndef DATA_FILE__H
#define DATA_FILE__H
#define data_file_local_identifier "file"

struct data_file
{
    char * file_name;
    char * local_identifier;

};

typedef struct data_file data_file; 

#endif /* DATA_FILE__H */

程序输出片段:

****_file_observational_area->_file 
***_file_observational_area->_file->local_identifier:file
***_file_observational_area->_file->file_name:A0087_0008_597249118_597252671_181130002623_eu.csv
***data_file_local_identifier:file
***_file_observational_area NOT A file 

在这行代码上面的get_data函数中:

      if (strcmp(_file_observational_area->_file->local_identifier, data_file_local_identifier) == 1)

Returns false 而不是 true,尽管如果您查看输出,两个字符串都是 "file"。那为什么会失败呢?

您似乎假设 strcmp returns 1 如果字符串相等。

但是strcmpreturns0 if strings are equal, or a number whose sign represents which is "smaller":

Returns an integral value indicating the relationship between the strings:

<0 the first character that does not match has a lower value in ptr1 than in ptr2

0 the contents of both strings are equal

>0 the first character that does not match has a greater value in ptr1 than in ptr2

strcmp returns 0 当字符串相等时,当它们不相等时为非零值。

要检查是否相等,请使用

if (strcmp(a, b) == 0) {

来自C标准(7.23.4.2 strcmp函数)

3 The strcmp function returns an integer greater than, equal to, or less than zero, accordingly as the string pointed to by s1 is greater than, equal to, or less than the string pointed to by s2.

所以如果你想检查两个字符串是否相等那么你应该写

if ( strcmp( s1, s2 ) == 0 )
{
    // ...
}

或者

if ( !strcmp( s1, s2 ) )
{
    // ...
}

尽管这样的条件会让读者感到困惑。

如果您查看 strcmp 文档,您会发现此函数的 return 值。

我相信你应该与 0 比较来测试相等的字符串。 >0 和 <0 表示不同的东西。

将“== 1”更改为“== 0”

strcmp(_file_observational_area->_file->local_identifier,data_file_local_identifier) == 0

http://www.cplusplus.com/reference/cstring/strcmp/