If the file name only has to include
"P5"
anywhere, you would write
if(temp.find(comp) != string::npos)...
But you mentioned a file
type, so maybe you want the file name to
end in ".P5". If so, define
comp
as
".P5"
and write
if(temp.rfind(comp) == (temp.size() - comp.size()))...
That is, if
temp
is
"foo.P5"
, its size (length) is 6 characters, and the size of
comp
is 3, so the match must occur at position 3, which is the
'.'
when counting up from
0
.
However, wouldn't
"p5"
also be valid? Whether to treat upper and lower case the same is often a consideration when dealing with strings.
And if the user actually
provides temp
, why not just append
".p5"
to it rather than force them to type it in if it's the only acceptable file type?
EDIT: Changed
.find
to
.rfind
in the second code fragment to allow ".p5" to occur earlier in the file name.