Search code examples
c++if-statementc-stringsstrcmp

strcmp returns false for for equal char sequence


I have a function that use strcmp to make a decision, see below:

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

}

The data_file_local_identifier that is used for comparison is defined in 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 */

program output snippet:

****_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 

In the get_data function above this line of code:

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

Returns false instead of true, although if you look at output both string are "file". So why is this failing?


Solution

  • You seem to assume that strcmp returns 1 if strings are equal.

    But strcmp returns 0 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