EvilZone

Programming and Scripting => C - C++ => : Kulverstukas December 18, 2012, 11:12:34 AM

: [C] get file name from an absolute or relative paths
: Kulverstukas December 18, 2012, 11:12:34 AM
With the help of few people I was able to produce this for my upcoming projects.
Since there are no functions to get the filename from a path (not that I know of anyway) I made my own - very useful for printing usage help.

: (c)
void getProgramName(char * progName, char * res) {
    int i;
    int argLen = strlen(progName)-1;
    int slashPos = 0;
    int counter = 0;
    // this steps through each char checking if it's a slash
    // stores slash position for later use
    // and determine how many chars are there from last found slash to end of str
    for (i = 0; i <= argLen; i++) {
        char c = progName[i];
        counter++;
        if ((c == 47) || (c == 92)) {
            slashPos = i;
            counter = 0;
        }
    }
    int src = slashPos;
    // copy chars from last found slash to the end
    for (i = 0; i <= counter; i++) {
        res[i] = progName[src];
        src++;
    }
}

Usage:
: (c)
    char res[255];
    getProgramName(argv[0], &res);

    printf("%s",res);

So, if you pass in "C:\Folder\program.exe", you will get "program.exe".
: Re: [C] get file name from an absolute or relative paths
: s3my0n December 18, 2012, 01:10:22 PM
Well, you can use the strtok function that's in the standard library to accomplish this easier :P

: (C)
#include <stdio.h>
#include <string.h>

char *getProgName(char *path);

int main(int argc, char *argv[])
{
    if (argc != 2) {
        printf("Need path to a file as an argument!\n");
        return 1;
    }

    printf("%s\n", getProgName(argv[1]));

    return 0;
}

char *getProgName(char *path)
{
    char *name;
    char *token = strtok(path, "/\\");
    while (token != NULL) {
        name = token;
        token = strtok(NULL, "/\\");
    }
    return name;
}
: Re: [C] get file name from an absolute or relative paths
: Polyphony December 18, 2012, 02:09:38 PM
Thanks guys, I wasn't looking for this at the moment but I saved the code in my c folder lol
: Re: [C] get file name from an absolute or relative paths
: Xires January 15, 2013, 05:43:46 AM
@s3my0n; Try to avoid returning improperly allocated pointers.  It's far too easy for a pointer to be invalidated somewhere else in the code, often somehow due to variable scope, and a segfault can occur when information is read from or written to that invalid pointer.  If you must return a pointer, malloc()(or calloc()) it and insist that it be free()'d.  Much of the time it's best to accept as an argument a pointer to a section of memory where the data should be written so that the variable scope control is left to the caller(and thus left to the responsibility of the function user, not the function writer).

@Kulverstukas; You may consider returning an 'int' to tell the function user whether there was or wasn't an error.  You could also return a positive value(0 included) for the character index(0..n-1) at which the last FS(field separator; '/' or '\', in this case) was found and a negative value if it was not found.  On the other hand, you could set it to unsigned int and return the character number(1..n) at which the last FS was found and 0 if it was not found.