if (char array != "something") { not working please help };

Hi guys, first of all I can only provide a snippet as my sketch is huge, it wont fit in a post and I dont expect anyone to go wading through it.

I am having problems with one small piece. I am combining two strings using sprintf with a couple of slashes and a .txt on the end to use as a file path to write to SD. It works perfectly, the only problem is if nothing is stored in my char arrays then I end up writing a file called //.txt.

To try and get around this I have placed my function call inside an if statement that should only be true if the file path is not equal to //.txt.

It does not wat to work, could anyone tell me what is going wrong here?

 case LOG_SAVE_BTN:
      snprintf(logPath, MAX_STR, "/%s/%s.txt", logFolder, logName);
      Serial.println (logPath);
        if (logPath != "//.txt") {
          saveLogFile(SD, logPath);
        }
        break;

Thanks in advance :slight_smile:

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

This line:

if (logPath != "//.txt") {

Is basically asking "Is the memory address of logPath the same as the memory address of "//txt". It is comparing to pointers, NOT two character strings. "==" works with Strings, not with arrays of char.

You need to use strcmp().

Thanks for the explanation and the tips, I got it working

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.