Ok i have a char with ''/afile.html" assigned to it. The extensions of it can vary so i need to get the extension to a seperate char to compare it with something else il show you what i have.... Basically i need everything after the '.'

char resource[16];
n = sscanf(buffer,"%s %s %s", req, resource, proto);

// This extracts the resource from the buffer
// Now i need to get the extensions of the reource char to use later

Recommended Answers

All 9 Replies

Why not store everything up to the '.' in one array, then everything after it in another array or a switch-controlled set of string literals? That way you can just use the program logic to output the file name then decide which extension to output...

Or am I missing something? Your code snippet doesn't seem very helpful.

Thats what i need to get, I need everything written after the last occurence of the '.'.

I guess I misunderstood the OP. Is this incoming information that you need to analyze, not output information?

I use _splitpath() under Windows. If you are after a full URL splitter, then look into WinINet's InternetCrackURL() function.

Otherwise use ANSIC's strrchr() function to find the ''/' and then use it again to find the '.'

This is incoming information from a web client and im writing a web server and i can recieve anything asdsadas.jpg index.html etc and i need to know the extension to send a response back to the client with the conent-type but i need to the extension first just as a string or char or char *, I just need the last few characters after the dot

I use _splitpath() under Windows. If you are after a full URL splitter, then look into WinINet's InternetCrackURL() function.

Otherwise use ANSIC's strrchr() function to find the ''/' and then use it again to find the '.'

How do i use the strrchr because i have tried using it before and i have not had luck.

Hmmm.... working with char arrays is not yet one of my strong points. I'm still figuring them out myself. I'll have to get back with if someone else doesn't first...

At this point, all I can suggest is using strrchr(str, ch) (note the 2 'r's there is a difference) in combination with strlen(). This would get a pointer to the last occurance of ch. You could then use a loop to get the remaining characters from the input.

I think this could lead to issues with incoming queries though...

commented: Ty +1

These might help you. Use these with stricmp() or strcpy() and you can change extentions, filenames or extract directories.

#include <string.h>

char* FindFileName(char* filename)
{
  char* result = strrchr(filename, '/');
  return result ? result + 1 : filename;
}

char* FindExtention(char* filename)
{
  char* result = strrchr(FindFileName(filename), '.');
  return result ? result : strchr(filename, 0);
}
commented: Ty +1

AlienKinetics you are a legend ty, Also thanks fbody.

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.