Author Topic: [C] get file name from an absolute or relative paths  (Read 4455 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
[C] get file name from an absolute or relative paths
« on: 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.

Code: (c) [Select]
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:
Code: (c) [Select]
    char res[255];
    getProgramName(argv[0], &res);

    printf("%s",res);

So, if you pass in "C:\Folder\program.exe", you will get "program.exe".
« Last Edit: December 18, 2012, 12:54:35 pm by Kulverstukas »

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [C] get file name from an absolute or relative paths
« Reply #1 on: 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

Code: (C) [Select]
#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;
}
Easter egg in all *nix systems: E(){ E|E& };E

Offline Polyphony

  • VIP
  • Knight
  • *
  • Posts: 178
  • Cookies: 23
    • View Profile
Re: [C] get file name from an absolute or relative paths
« Reply #2 on: 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
Code: [Select]
<Spacecow_> for that matter I have trouble believing bitches are made out of ribs
<Gundilido> we are the revolutionary vanguard fighting for the peoples right to display sombrero dawning poultry
<Spacecow> did they see your doodle?
<~phage> Maybe
<+Unresolved> its just not creative enough for me
<+Unresolved> my imagination is to big to something so simple

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [C] get file name from an absolute or relative paths
« Reply #3 on: 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.
-Xires