How can I check whether a string ends with .csv
in C?
I've tried using strlen
without any success.
どうですか:
char *dot = strrchr(str, '.');
if (dot && !strcmp(dot, ".csv"))
/* ... */
if(strlen(str) > 4 && !strcmp(str + strlen(str) - 4, ".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));
}