I'm trying to strip only path from a c string

char *path = "~/folder/sometext.txt"

so I would get only "~/folder/"

Anyone got a hint on how to achieve this? I was thinking of something similar to std::string find_last_of
so I could just indicate the last "/" or "\" in the string and then anything that comes after that will
be removed from the string. But I'm writing this in c only so c++ solutions are not an option.

Recommended Answers

All 2 Replies

Member Avatar for MonsieurPointer

Check out strrchr; it will return a pointer to the last char you passed on to this function. You can then strip off that part from path.

Nice this is exactly what I was looking for, thank you MonsieurPointer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.