can someone fix my function? (small one)

Started by
14 comments, last by sporty 22 years, 4 months ago
  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;}  
Advertisement
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
>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 isn''t a proper cstring–it isn''t terminated by ''\0''. You could just change the body of this loop to:<br><br>sztmp = file[ofs+i];<br><br>and then after the loop put:<br><br>sztmp[len] = ''\0'';<br><br>You''re also doing something similar in the following:<br><br>&gt;strcpy(&amp;returnFile, &amp;sztmp);<br><br>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.<br><br>The easiest way to do this is:<br><br><pre><br>void getfile(char *file, char *returnFile) {<br> int length, base, i;<br><br> base = i = length-1;<br> for (;i &gt;= 0 &amp;&amp; file != ''/''; i–; base–<img src="wink.gif" width=15 height=15 align=middle>;<br> strcpy(returnFile, &amp;file[base]);<br>}<br> </pre> <br><br> </i>
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?

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.
I was going to mention strtok(); I''m not terribly up on Standard C Library string functions. Nice one, Beer Hunter.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement