Alex Rivera | Logout

how to remove extension from file name?

Asked 2010-04-29T11:09:12.567
10

I want to throw the last three character from file name and get the rest?

I have this code:

char* remove(char* mystr) {

    char tmp[] = {0};
    unsigned int x;

    for (x = 0; x < (strlen(mystr) - 3); x++)
        tmp[x] = mystr[x];

    return tmp;
}
Edit
Report

2 Answers

3

I would try the following algorithm:

last_dot = -1

for each char in str:
    if char = '.':
        last_dot = index(char)

if last_dot != -1:
    str[last_dot] = '\0'
answered 2010-04-29T11:17:25.917
1

To get paxdiablo's second more general purpose solution to work in a C++ compiler I changed this line:

if ((retstr = malloc (strlen (mystr) + 1)) == NULL)

to:

if ((retstr = static_cast<char*>(malloc (strlen (mystr) + 1))) == NULL)

Hope this helps someone.

answered 2010-06-11T10:29:59.843

Your Answer