Hello guys,
I have the following task to do:
given a string (char * buf) in a specific way ("putfile $[path of file]") I want to extract the filename out of the filepath with a function called extract_name.
Here's the code:
Code:
char* extract_name(char* buf){
int length = strlen(buf)-1;
int count = -1;
int i = 0;
char * fname;
char c;
for(i = length; i>=0; i--){ //go backwards through string to gain knowledge of filename length
c = buf[length];
if((c == '/') || (c == '\\') || (c == '$')){ //either of these chars show me I have reached the
//beginning of my filename, so I leave the loop
break;
}
count++;
length--;
}
fname = (char*)calloc(count+1, sizeof(char)); //allocate free space
fname[count+1] = '\0'; //put string termination symbol
length = strlen(buf)-1; //reset length
for(i = count; i>=0; i--){ //go backwards through string
fname[i] = buf[length]; //and set the characters accordingly
i--;
length--;
}
return fname; //return pointer to name
}
Now, my problem is, that I get no filename back. It's just empty.
Maybe you need to see this part of code too?
Code:
char *name;
name = extract_name(message);
path = (char*)calloc(10 + strlen(act_user->username) + strlen(name), sizeof(char));
sprintf(path, "./uploads/%s/%s",act_user->username,name); //create path-string
Anyone can tell me where exactly my error is? Or should I do it in a different way alltogether?