How do I remove the file Extension from a string?
#1

Hi community

I have a string containing the full path and name of a file, and I want to remove everything behind the last '.', how can I do that?

Thanks for your time
Reply
#2

Do you mean something like strdel?
Reply
#3

Only using PAWN I don't believe that be possible.
Reply
#4

If not with pawn, do you know a way to make that in C++, so I can plug that function in?
Reply
#5

Quote:
Originally Posted by [GF]Sasino97
Посмотреть сообщение
If not with pawn, do you know a way to make that in C++, so I can plug that function in?
why can't you use the strdel I gave you?
Reply
#6

Quote:
Originally Posted by [GF]Sasino97
Посмотреть сообщение
If not with pawn, do you know a way to make that in C++, so I can plug that function in?
The ****** is your friend:

http://stackoverflow.com/questions/6...-up-for-review

Replace sprintf to sprintf_s for get security, and for linux sprintf don't work, another function is needed, or just remove it, delete the last char and rename the file.

pawn Код:
#if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__
    #define format snprintf
#endif

#if defined __WIN32__ || defined _WIN32 || defined WIN32
    #define format sprintf_s
#endif
Reply
#7

If you have only 1 dot in the filename, then it's easy enough;
pawn Код:
new filename[32] = example.txt,
strdel(filename, strfind(filename, ".", true), strlen(filename))
Reply
#8

pawn Код:
main() {
    // test
    new path[] = "path/to/file/with.periods.ext";
    remove_extension(path);
    printf("path = %s", path);
}

stock delete_extension(path[]) {
    new i = -1, j;
    // Loop until find the last '.'
    while ((j = strfind(path, ".", _, i + 1)) != -1) {
        i = j;
    }
    // Delete .extension
    strdel(path, i, i + strlen(path[i]));
}
Reply
#9

I was using my know to help him, I'm sorry for give wrong informations, you pointed that I'm Less by You, thanks for this, I'm not flaming, is a positive thing.
Reply
#10

Thanks dudes ! You all helped me Reputation points ++ to you
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)