Hello
I am trying to use a method that extracts the filename out of a filepath but I cant seem to make it work. The code worked ok within another function, but now when I try to separate it into its own function it doesnt work anymore. =/
This is my function
Code:
#include "gbs.h"
char * extract_filename(char *str){
int ch = '\\', result, len;
char *pdest, line[250], *inpfile = {NULL};
// Search backwards for last backslash in filepath /
inpfile = (char *)malloc(200*sizeof(char));
pdest = strrchr(str, ch); result = pdest - str + 1;
// if backslash not found in filepath
if(pdest == NULL ){
printf( "Result:\t%c not found\n" );
}
// extract filename from file path /
len = strlen(str) - result - 1;
strncpy(inpfile, &line[result], len);
inpfile[len] = NULL;
return inpfile;
}
The function is declared in the header gbs.h. I try to call the function from another function like this. I omitted most of the code to not make the post shorter.
Code:
void anotherfunction()
{
char *filename, filepath[200];
//some code
fscanf(fp,"%s",filepath);
printf("%s\n",filepath);
filename= extract_filename(filepath);
printf("%s\n",filename);
}
The printf("%s\n",filepath); is printed OK, but the output from printf("%s\n",filename); is this: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠kkkk
I am new to this and I would be very thankful for any ideas!