Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

sporty

can someone fix my function? (small one)

This topic is 6066 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im trying to write a function that will return just the filename from a file path (folders notated by FOWARD SLASHES ''/''), in other words, "c:/windows/desktop/blah/hello.bmp" would return "hello.bmp" my function worked, only it created a memory leak, so im trying to pass a variable to the function now and have it modify it, but i am stumped..this is my code
  
//file being the filepath, i.e "c:/folder/filename.xxx"

//returnfile being the string that returns the filename

void getfile(char *file, char *&returnFile) {
	int ofs=0, i, len;
	int l;
	char *sztmp;
	l = strlen(file);
	for (i=0; i<l; i++) {
		if (file[i] == ''/'') ofs=i+1;
	}
	len=l-ofs;
	sztmp = new char[len];
	for (i=0; i<len; i++) {
		strcpy(&sztmp[i], &file[ofs+i]);
	}
	strcpy(&returnFile, &sztmp);
	delete [] sztmp;
}

void main() {
   char filename[56];
   getfile("c:/windows/desktop/hello.bmp", filename);
   cout << filename << endl;
}
  
unfortunately and obviously this isn''t working, heh, so can you guys show me what i _should_ be doing?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
It''s not as complicated as all that

// get a pointer to start of filename
LPTSTR lpszTemp = inputfilename

// prep memory buffers as fitting
LPTSTR filename;
LPTSTR fileext;

// split off filename
while( *lpszTemp != ''.'' )
{
*filename++ = *lpszTemp++;
}

// split off file extension
while( *lpszTemp != ''\0'' )
{
*fileext++ = *lpszTemp++;
}

Share this post


Link to post
Share on other sites
one problem is that char* &returnfile should just be char* returnFile. I think that''s right, but if it compiles it probably doesn''t matter anyway

Share this post


Link to post
Share on other sites
  
#include <string>
using namespace std;

void getfile( string& path, string& file )
{
file = path;
file.erase( 0, file.rfind( ''/'' ) ); // erases the string up to the last ''/''

}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If you are using MSVC there is a function splitpath(...) or _splitpath(...) that should help. If you are using gcc or unix variants, i think there is a function called fnsplit. try stdlib.h (for msvc) and i''m not sure where it''s located in unix.

Share this post


Link to post
Share on other sites
How about

        
void getfile(char *file, char *returnFile)
{
int i = strlen( file );
while( i > 0 )
{
if( file[--i] == '/' ) { ++i; break; }
}
strcpy( returnFile, file+i );
}


You may need to check if it actually works


Edited by - Fruny on December 11, 2001 10:30:06 PM

Share this post


Link to post
Share on other sites
*sigh*

the ability to extract my filename is not the problem here. it works. im content with that code.

my problem is returning the value to the second parameter.

Share this post


Link to post
Share on other sites
You can always use memmove() which WILL handle overlapping strings so that you can do strcpy(&file[ i ], &file[ofs+i]) or something like that.
That will remove the need for the dynamic local buffer.

Edit: stupid me, why didn't you just do a strcpy from file to returnFile in the first place ?

Edited by - Fruny on December 11, 2001 10:36:32 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!