strstr whole string match
Posted
by clay
on Stack Overflow
See other posts from Stack Overflow
or by clay
Published on 2010-04-19T03:51:15Z
Indexed on
2010/04/19
3:53 UTC
Read the original article
Hit count: 206
I'm trying to match the whole string and not just part of it. For instance, if the needle
is 2
, I would like to match just the string 2
and not 20, 02, or 22
or anything related.
I'm using strstr
as:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *file;
char l[BUFSIZ];
int linenumber = 1;
char term[6] = "2";
file = fopen(argv[1], "r");
if(file != NULL) {
while(fgets(l, sizeof(l), file)){
if(strstr(l, term) != NULL) {
printf("Search Term Found at %d!\n", linenumber);
}
++linenumber;
}
}
else {
perror(argv[1]);
}
fclose(file);
return 0;
}
© Stack Overflow or respective owner