1

Can any one please tell me how to implement GetFileName() function in C without using string inbuilt function of C. For example C:\Program Files\hello.txt

output : hello.txt

Avishi
  • 19
  • 2
  • 4
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). You might also want to learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Sep 09 '15 at 11:04
  • 2
    @Uday: Yes, but that's a built-in string function, which OP doesn't want to use. – M Oehm Sep 09 '15 at 11:11
  • The filename and function names are identical to what was asked and answered in http://stackoverflow.com/questions/6921105/get-file-name-from-a-path-string-in-c-sharp. Did you start with this question? Please note that you are speaking about C (not C#) which doesn't have a `GetFileName()` function nor a `string` class... – Daniel Alder Sep 09 '15 at 11:19

3 Answers3

3

Loop through the string looking for the last separator. If /, \ and : are valid path separators (Windows):

char *getFileName(char *path) {
    char *retVal = path, p;
    for (p = path; *p; p++) {
        if (*p == '/' || *p == '\\' || *p == ':') {
            retVal = p;
        }
    }
    return retVal;
}
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
  • I'd implement to versions, one for windows one for unix. – alk Sep 09 '15 at 12:15
  • with above code value of retVal is coming ": Program Files hello.txt" but output required only filename means "hello.txt" – Avishi Sep 09 '15 at 12:34
  • 1
    Did you put the string in your code? Remember that you need to escape `\ `. The string will be `char path[] = "C:\\Program Files\\hello.txt"` in a C program. – Klas Lindbäck Sep 09 '15 at 12:46
2

Without using built-in string functions, I assume you can't use strlen or strdup. The simplest way you can achieve that is:

char *fname(char *path)
{
    char *aux = path;

    /* Go to end of string, so you don't need strlen */
    while (*path++) ;

    /* Find the last occurence of \ */
    while (*path-- != '\\' && path != aux) ;

    /* It must ignore the \ */
    return (aux == path) ? path : path + 2;
}




EDIT: Thanks to Klas Lindbäck for pointing out that the original function had no boundary check and would fail in case the provided path had no occurrence of \\.
Community
  • 1
  • 1
Enzo Ferber
  • 3,029
  • 1
  • 14
  • 24
0

Starting at the end of the path you can copy characters from the path to another string until you find the '\' character. So after the while loop the reverseFileName string will be 'txt.olleH'. The for loop reverses the 'txt.olleH' to 'Hello.txt'

char* GetFileName(char[] path)
{
      char *reverseFileName = malloc(100*sizeof(char));
      int len = strlen(path);
      int i=len-1;
      int j=0;
      while( (path[i] != '\') && (i>=0) )
      {
        reverseFileName[j] = fileName[i];
        i--;
        j++;
      }
      reverseFileName[j+1]='\0';
      int reverseLength = strlen(reverseFileName)
      char *fileName = malloc( (reverseLength+1)*sizeof(char) );
      j=0;
      for(i=reverseLength-1; i>=0; i++)
      {
        fileName[j] = reverseFileName[i];
        j++;
      }
      free(reverseFileName);
      return fileName;
 }
dimlucas
  • 5,040
  • 7
  • 37
  • 54