else statement skipped when it shouldn't be

So I have a weird problem that I can't wrap my head around. I have a if...else if...else block that doesn't execute the else statement when both the if and else if statements are false.

int load(void)
{
    int index = 0;

    // temp string buffer for characters.
    _TCHAR* str = calloc(sizeof(TCHAR), 127);


    // open config file.
    FILE* fp = fopen("config.txt", "r");
    // if file is NULL, return.
    if(fp == NULL)
    {
        fprintf(stderr, "Could not open config.txt\n");
        return 1;
    }

    do
    {
        int c;
        // get characters from the file and put them in the message buffer.
        do
        {
            c = fgetc(fp);
            // if new line found end word.
            if(c == '\n')
            {
                str[index] = '\0';
                break;
            }
            // if EOF return.
            else if(c == EOF)
                return 0;

            // else keep storing characters.
            else
            {
                str[index] = c;
                index++;
            }
        }while(1);

        // time specifier is found.
        if((str[0] == 't' || 'd' || 'c') && (isdigit(str[1]) != 0))
        {
            // if spec. is 't' store the time of day.
            if(str[0] == 't')
            {
                if(StoreTime(str) != 0)
                {
                    fprintf(stderr, "Time of day could not be stored.\n");
                    return 1;
                }
            }
            // if spec. is 'd' store the date.
            else if(str[0] == 'd')
            {
                if(StoreDate(str) != 0)
                {
                    fprintf(stderr, "Date could not be stored.\n");
                    return 1;
                }
            }
            // else spec. is 'c'.
            else
            {
                if(StoreCount(&str[1]) != 0)
                {
                    fprintf(stderr, "Countdown could not be stored.\n");
                    return 1;
                }
            }
        }
        // comment text or new line found.
        else if(str[0] == '*' || '\n')
        {
            // do nothing
        }
        // message found.
        else
        {
            if(StoreText(str) != 0)
            {
                fprintf(stderr, "Text could not be stored.\n");
                return 1;
            }
        }

            index = 0;
    }while(1);

fclose(fp);
return 0;

}

And i have some if statements inside those as well mostly as function return checks but that should not affect the outermost statements if I'm not totally dizzy right now. But this code still skips the else statement.

When i run it with GDB it gets the value that i want, that doesn't trigger the first if statement or the else if statement. And then it just skips right down to the "do stuff regardless" part. So i have no idea what's going on.

EDIT* added real code

Answers


In a couple of places you attempt to test for multiple values like this:

if (str[0] == 't' || 'd' || 'c')

That code is legal but it doesn't do what you want. You need separate tests for each value like this:

if (str[0] == 't' || str[0] == 'd' || str[0] == 'c')

Need Your Help

Get user-specific web-content

liferay webcontent

I would like to ask if there is any Java API call in liferay which returns the web contents, which were uploaded by a specific user.

Flyway integration in a production database

database migration flyway

We have a database in production that already has a good number of rows in the "user" table. Consider the following statement from the flyway website:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.