![]() |
| | #1 |
| Registered User Join Date: Jan 2009
Posts: 3
| extracting a specific part out of a string (errors in my code?) 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
}
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
|
| snpnx is offline | |
| | #2 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| Yarr ugly! Code: #include <stdio.h>
#include <stdlib.h>
#define MAX_PATH 256
char * extract_name(char * buf)
{
char * path = NULL;
path = malloc(MAX_PATH);
if(!path)
return NULL;
sscanf(buf, "%*s %256s", path);
return path;
}
int main(void)
{
char * a;
a = extract_name("putfile /foo/bar");
if(a)
{
printf("%s\n", a);
free(a);
}
return 0;
}
1. You don't need to traverse backwards. You know the length of the filename is the length of the buffer - the start bit. 2. You can use strncpy() you know .In reality I'd probably strip all the trailing and leading spaces from the buffer, then again from the path after I parsed it. Probably with the above method if I was feeling lazy Last edited by zacs7; 01-15-2009 at 06:34 AM. |
| zacs7 is offline | |
| | #3 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| You should be aware of strrchr: Code: #include <stdio.h>
#include <string.h>
char *extract_name (char *fpathname) {
char *ptr=strrchr(fpathname,'/');
ptr++;
return ptr;
}
int main() {
char example[]="/home/user/somedir/afile.o",
*shortname=extract_name(example);
printf("%s\n",shortname);
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 01-15-2009 at 06:44 AM. |
| MK27 is online now | |
| | #5 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| I really don't think you should do this: Quote:
Code: #ifdef WIN32 #define SC 92 /* a "\" */ #else #define SC 47 /* a "/" */ #endif [...] char *ptr=strrchr(fpathname,SC);
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #6 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Well, that's true. Also, blue is not the same as green. Is this relevent?
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #7 |
| Registered User Join Date: Jan 2009
Posts: 3
| @zacs7: why no use of calloc? @MK27: an honest question -- what OS allows "$" in filenames or paths? Not a question of the OS, it was just needed since I did go backwards through the String and thought if the user puts in the filename without any leading './' I know it's right after the '$'. But I'll use strrchr, which I didn't know and let the user know that he needs to give me a '/' with the filename. ![]() @all Thanks for your help, it should be enough for me to figure it out. If I have questions again, I'll come back |
| snpnx is offline | |
| | #10 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
Pick, pick pick...anyway good luck
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #11 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: for(i = count; i>=0; i--){ //go backwards through string
fname[i] = buf[length]; //and set the characters accordingly
i--;
length--;
}
Code: fname = (char*)calloc(count+1, sizeof(char)); //allocate free space fname[count+1] = '\0'; //put string termination symbol -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #12 | ||
| Registered User Join Date: Jan 2009
Posts: 3
| Quote:
Quote:
| ||
| snpnx is offline | |
| | #13 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
Code: prompt$ /home/user/file.txt
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
![]() |
| Tags |
| char, extract, pointer, string |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Error in Recursive String Palindrome Code | clegs | C Programming | 13 | 12-21-2008 12:36 PM |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |
| Can someone help me understand this example program | Guti14 | C Programming | 6 | 09-06-2004 12:19 PM |
| Linked List Help | CJ7Mudrover | C Programming | 9 | 03-10-2004 10:33 PM |
| True ASM vs. Fake ASM ???? | DavidP | A Brief History of Cprogramming.com | 7 | 04-02-2003 04:28 AM |