compare chain of chars

Hi ;)

I have a simple question.
I want to compare the end of file "*.txt" or "*.cxx" ?
I need to use function to compare a few characters in string.

const char* filename = "file.txt";

if(strstr(filename, ".txt") != 0)
{
std::cout<<"It is cxx file"<<std::endl;

....

} else
{
std::cout<<" it is txt file"<<std::endl;

....
}

I would appreciate for any help please ;)
What is your problem?

Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
closed account (j3A5Djzh)
Hi, this should work.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstring>

const char* filename = "file.txt";

int main(void)
{
    if(strcmp(filename + strlen(filename) - 4, ".txt") != 0)
        std::cout<<"It is cxx file"<<std::endl;
    else
        std::cout<<"It is txt file"<<std::endl;
    return 0;
}
Topic archived. No new replies allowed.