/** returns non-zero if filename ends in a dot followed by extension,
else returns 0 ****/
#include <string.h>
int extension_ok(const char *filename, const char *extension) {
char *ptr;
if(!(ptr = strrchr(filename,'.')) /* ptr points to either last
'dot' in filename, or null if filename doesn't have a 'dot' */
return 0; /* there was no dot in the
string */
return (!strcmp(++ptr,extension));
}
use it like this:
if(!extension_ok(filename,"xyz"))
puts("File must end in 'xyz'");
else
...
Ibrahim F Haddad wrote:
> Hello All,
>
> Given I hold the user file name in a char *fileName.
> I need to check whether the file is of extension xyz (i.e. name.xyz).
> I determine the lenght of the fileName (strlen(fileName)).
> How can i then check if the last 4 characters are .xyz in a neat
> way. Cause if they're not i will return a warning message.
>
> Thank you.
>
> - Ibrahim Haddad