LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C - How to put a specific arbitrary part of a string into it's own string? (https://www.linuxquestions.org/questions/programming-9/c-how-to-put-a-specific-arbitrary-part-of-a-string-into-its-own-string-802715/)

golmschenk 04-18-2010 01:24 PM

C - How to put a specific arbitrary part of a string into it's own string?
 
So if I'm given a location of a file like:
/something/somethingelse/thefile.html or
/different/moredifferent/otherfile.jpg
How can I just take the type of the file at the end? I know I can use strrchr() for a period to get the pointer to the period just before file type. Is there a build in string function that will just take the rest of the string from a certain point on forward in the string? I know it wouldn't be much work to make it myself, but I figured I would find out if it already existed before doing it.

Thanks!

troop 04-18-2010 01:51 PM

too simpe for the special function
Code:

char str[]="/something/somethingelse/thefile.html";
char str_ext[10];
strcpy(str_ext,((char*)strrchr(str,'.'))+1);


golmschenk 04-18-2010 02:26 PM

Great great, thanks thanks.

jiml8 04-18-2010 02:32 PM

and how well will that scheme work if char str[]="/some.thing/some.thing.else/thefile.html"; ?

Best way it to use pointers and scan backward through the string to find the last period in it.

troop 04-18-2010 02:56 PM

Quote:

Originally Posted by jiml8 (Post 3939637)
and how well will that scheme work if char str[]="/some.thing/some.thing.else/thefile.html"; ?

that scheme work very well. output: html
Quote:

Best way it to use pointers and scan backward through the string to find the last period in it.
strrchr!=strchr

jiml8 04-18-2010 06:36 PM

Oops. I missed the extra "r" in the middle of the function. :D

grail 04-18-2010 07:28 PM

OP - if you have your solution, please mark query as SOLVED.

golmschenk 04-19-2010 10:12 AM

Oops, sorry about not marking solved. I usually remember that.

dmail 04-19-2010 11:05 AM

Quote:

Originally Posted by grail (Post 3939801)
OP - if you have your solution, please mark query as SOLVED.

Is it solved?

"/foo/.baz"
"./foo"
"/foo.causeBufferOverFlow"

Marking threads as solved is wrong, especially since the person who marks it as such does not have the knowledge to know if there are problems. I will purposely leave this as an exercise.

grail 04-19-2010 08:27 PM

Whilst i understand your point dmail, I also cannot tell if the OP understands that the solution is not final but may need
some more work just as it may be all they need to solve this particular issue.

But as OPs last post was:
Quote:

Great great, thanks thanks.
I just took it that they probably were not going to revisit this query as they seemed to be satisfied.


All times are GMT -5. The time now is 02:10 AM.