How can I check whether a string ends with ".csv" in C
How about:
char *dot = strrchr(str, '.');
if (dot && !strcmp(dot, ".csv"))
/* ... */
The simplest (and most general) form of ThiefMaster's code would be:
int string_ends_with(const char * str, const char * suffix)
{
int str_len = strlen(str);
int suffix_len = strlen(suffix);
return
(str_len >= suffix_len) &&
(0 == strcmp(str + (str_len-suffix_len), suffix));
}
if(strlen(str) > 4 && !strcmp(str + strlen(str) - 4, ".csv"))