Archived

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

sporty

can someone fix my function? (small one)

Recommended Posts

sporty    122
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
Guest Anonymous Poster   
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
masonium    118
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
Fruny    1658
  
#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   
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
Fruny    1658
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
sporty    122
*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
Fruny    1658
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
JonStelly    127
  
bool getfile(char* lpFullFileName, char** plpFileName)
{
//FIND LAST DIRECTORY DELIM

(*plpFileName) = strrchr(lpFullFileName, ''/'');

//''/'' CHARACTER FOUND

if(*plpFileName != NULL){
//MOVED TO NEXT CHARACTER

(*plpFileName)++;
return true;
}

//''/'' CHARACTER NOT FOUND

return false;
}

int main(int argc, char* argv[])
{
char *lpFile;
getfile("c:/testdir/test.txt", &lpFile);
cout << lpFile << endl;
return 0;
}

Share this post


Link to post
Share on other sites
scaught    122
This is the part that gets me:

  
...
for (i=0; i<len; i++) {
strcpy(&sztmp[i], &file[ofs+i]);
}
...


Holy extraneous copies, batman...


Edited by - scaught on December 12, 2001 1:15:59 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
>for (i=0; i<len; i++) {
> strcpy(&sztmp, &file[ofs+i]);
>}

Why are you iterating through file and doing an strcpy on each character? You''re dereferencing each character in the string and trying to use strcpy on it but &sztmp[i] isn''t a proper cstring--it isn''t terminated by ''\0''. You could just change the body of this loop to:

sztmp[i] = file[ofs+i];

and then after the loop put:

sztmp[len] = ''\0'';

You''re also doing something similar in the following:

>strcpy(&returnFile, &sztmp);

Here you''re passing a pointer to a pointer a char for each parameter. You shouldn''t be dereferencing returnFile or sztemp. Checkout the documentation on strcpy.

The easiest way to do this is:


void getfile(char *file, char *returnFile) {
int length, base, i;

base = i = length-1;
for (;i >= 0 && file[i] != ''/''; i--; base--;
strcpy(returnFile, &file[base]);
}


Share this post


Link to post
Share on other sites
amemorex    122
quote:
Original post by JonStelly

    
bool getfile(char* lpFullFileName, char** plpFileName)



whoa there, what does "char**" do? and whats it mean when all of you are talking about "derefrencing your variables"..how do you DEreference something?

Share this post


Link to post
Share on other sites
Beer Hunter    712
int PositionOfLastSlash = strrchr("c:/fun/blah/etc/nifty.bmp", ''/'');

You can use that to simplify things a bit. I''m surprised that no-one has mentioned that yet.

Share this post


Link to post
Share on other sites