Sign in to follow this  
pex22

checking file extension

Recommended Posts

hi, i made a file extension checker function and it compiles fine. but i cant run an application that uses that function. when i run the app, the app quits automatically with the following error: "gtester.exe has encountered a problem and needs to close. We are sorry for the inconvenience." here is my checker function:
char* Shorts::checkExtension(char* string)
	{
	  char* dot;
	  int   pos;
	  char* ext;
	  dot = strrchr(string,'.');
	  pos = dot - string + 1;
	  while (string[pos] != '\0')
		  ext = strcat(ext,&string[pos]);
	  return ext;
	};
where's my mistake? thanks, pex.

Share this post


Link to post
Share on other sites
Why don't you use a file ID at the beginning of the file to find out if its the right file type instead of doing a check on the file extension.... That way people can rename it without the file extension and it would still work.

Share this post


Link to post
Share on other sites
ext has no memory allocated to it, its just a pointer to nothing, strcat shouldn't need to be in that while loop, should only need to be called once. The function is also returning a pointer to a local object which wont exist after the function exits. Try something like this instead.


#include <cstring>
#include <iostream>

char* Shorts::checkExtension(char* string)
{
return std::strrchr(string,'.') + 1;
}

int main()
{
Shorts object;
std::cout << object.checkExtension("hello.txt");
}



Share this post


Link to post
Share on other sites
because its not by my own
its used to load models, textures, etc. in one function (one function that will call other functions to loadtga (if its a tga file), loadmd2 (if its a md2 file), etc.).

@Jingo: thanks it works now :)

Share this post


Link to post
Share on other sites
You should really search backwards for the '.', and if you reach the start of the string or a '/' or '\\' character, then it doesn't have an extension.

I haven't tried this but blah:


const char * extension(const char *string)
{
const char *start = string;

for(; *string; ++string) {}

for(; string != start && string[-1] != '/' && string[-1] != '\\'; --string)
if(string[-1] == '.')
return string;

return "";
}

Share this post


Link to post
Share on other sites
I've just made several attempts at the exact same problem, this is the solution I'm using:

const char* ext = strrchr( string, '.' );
ext++;

done =)

don't understand why you did all the other stuff though. if you need to copy the stuff, then just continue with:

extLen = strlen( ext ) + 1; // don't forget the '/0'
char* extBuff = new char[ extLen ];
strcpy( extBuff, ext );

But I probably would prefer a the safer way of copying:
std::string extString( ext );

Hope I helped =)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this