Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a program on Windows in regular C using Visual Studio 2017.

I have the name of a file in this variable:
CHAR szFileName[MAX_PATH];
I need to determine if the file extension is a certain value. In vbscript it would be something like this:

If Right(szFilename, 3) = ".txt" Then
... do something here ...
End If

Any suggestions on how to do this in regular C would be appreciated.

What I have tried:

I've done several web searches but haven't had any luck finding anything.
Posted
Updated 9-Jan-20 23:35pm

Get the length of the string minus by the length of the file extension string(4) to get to last 4th position and use strcmp to compare with the ".txt" extension, if the result is zero, it means matched, else it is different.

char szFileName[255] = "stories.txt";
if (strcmp(&szFileName[strlen(szFileName) - 4], ".txt") == 0)
    printf("same file extension\n");
else
    printf("different file extension\n");
 
Share this answer
 
Comments
John R. Shaw 9-Jan-20 23:25pm    
I actually like your solution better than mine. But there is one problem - do not hard code the number of characters in the extension.
A better solution:
return (strcmp(&szFileName[strlen(szFileName) - strlen(szExt), szExt);
CPallini 10-Jan-20 4:55am    
5.
Don't use hard coded numbers, or string lengths to find it; the simplest way is to use strrchr to find the last period (full stop) character in the filename, and compare the resulting substring.
C++
char* pszExtension = strrchr(szFileName, '.');
if (pszExtension != NULL && strcmp(pszExtension, ".txt") == 0)
{
    // the extension is .txt
}
else
{
    // not found ...
}


[edit]
Updated to search for the last dot. Thanks Carlo.
[/edit]
 
Share this answer
 
v2
Comments
CPallini 10-Jan-20 6:25am    
What about the 'foo.boo.goo.txt' file?
:-)
Richard MacCutchan 10-Jan-20 6:46am    
Thank you, solution updated.
CPallini 10-Jan-20 6:53am    
My 5.
Richard MacCutchan 10-Jan-20 6:59am    
Thank you again.
Roland M Smith 10-Jan-20 11:04am    
I'm going with this solution except I'm using _stricmp so that it is case-insensitive.
Simple - just determine the length of the string stored in szFileName and search backwards until you fine the '.' then compare that to the extension you are looking for. In other words use you brain, not the internet, to solve the problem.

The following old style C-code should do the trick:
C++
/* This code has not been compiled - but I would be surprised if is does not work as is. */
int HasExtension(const char* szFileName, const char* szExt)
{
    if ('\0' != *szFileName) /* required to prevent out of range error. */
    {
        int i;
        for( i = strlen(szFileName) - 1; i > 0; --i)
        {
            if ('.' == szFileName[i])
                break;
        }
    }
    return (0 == strcmp(szFileName + i, szExt));
}

This is not the only way to do this in C, but it will get the job done.

See if you can accomplish the same task with a while loop.

Do your research. The modern standard C library may have newer functions that will do this type of thing for you. A good C programmer should have the ability to write the standard library from scratch without using the internet.

After seeing Shao's answer I thought I would throw this improvement in. Please note that this has no built in protection against programmer error.
int StringEndsWith(const char* szFileName, const char* szExt)
{
    return (strcmp(&szFileName[strlen(szFileName) - strlen(szExt), szExt) == 0);
}
 
Share this answer
 
v8
Here is a tutorial on various string methods to help you out
https://www.codingame.com/playgrounds/14213/how-to-play-with-strings-in-c/string-length-string-comparison[^]

And another which seems to be something you could use for the question at hand
Right String – Solution | C For Dummies Blog[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900