Problem with strcmp

03 Feb 2011

Hi,

I'm experiencing problems with the call of strrchr:

to find a file by its extension I use the following function:

struct dirent* GetNextFileByExtension(DIR *pDirectory, const char* cExtension)
{
    struct dirent *pFile;
    while ( (pFile = readdir(pDirectory)) != NULL )
    {
        char* pExt = NULL;
        printf("%s \r\n", pFile->d_name);
        pExt = strrchr(pFile->d_name, '.');
        if(pExt != NULL)
        {
            printf("%s \r\n", pExt);
            if(strcmp(pExt + 1, cExtension)==0)
                return pFile;   
        }
        
    }
    return NULL;
}

If pExt is not NULL programm crashes. It must be the call of strcmp.. I tested the snipped on VS2005 on my pc an it works. Has anybody an idea where the problem is?

Thx

03 Feb 2011

you may have found the '.' but can you be sure it's null terminated beyond that?

03 Feb 2011

I've already checked that. the d_name entries are always null terminated

03 Feb 2011

strrchr() wouldn't work on a non-terminated string so that's not it.

Stefan, do you see the extension printed? Also, I wonder maybe it's cExtension that is corrupted somehow. Try printing it too?

03 Feb 2011

After updating all libraries it works now.. It seams as if this was the problem..