For what it is worth, using strlen
is much more efficient than using strtok
. I did some tests (omitting the file access) using strlen
, strchr
, strtok
, strstr
, strpbrk
and strcspn
:
It surprised me that to execute this neat looking thing:
s = strchr(s, '\n');
if (s) {
*s = '\0';
}
takes nearly 50% longer than this ugly looking sucker:
size_t len = strlen(s);
if (len && (s[len-1] == '\n')) {
s[len-1] = '\0';
}
and the beautiful
strtok(s, "\n\r");
and
strsep(&s, "\n\r");
are 10 to 20 times slower. The latter two do of course look for \r as well as \n. But even adding \r, the strlen
approach is still quicker than the strchr
and still quicker then strtok
and strsep
by 10 to 20 times:
size_t len = strlen(s);
if (len && ((s[len-1] == '\n') || (s[len-1] == '\r'))) {
s[len-1] = '\0';
}
Note that I wasn't exhaustive with string variations. In fact I didn't even break a sweat. And of course this is all quite irrelevant as the stdio
call will dominate in your target code :-)
Note also from the comments below that if the first character of the search string (s
in the examples above) is one of the characters in the pattern string, strtok
does nothing (thanks to @chux).