Thread: Question on parsing

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    19

    Question on parsing

    Hello--I am trying to get a program to parse a path, filename, and extension. Here's what I have so far:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    char *file_from_path(char *pathname)
    {     
    char *fname = NULL;
         if (pathname)
              {
               fname = strrchr (pathname, '/')+1;
              }
    return fname;
    }
    
    int main (void)
    {
    
    char pathname[] = "C:/Users/Michael/Desktop/File Directory/example.doc";
    char *fname = file_from_path(pathname);
    
    printf("path: \"%s\"\nfilename: \"%s\"\n", pathname, fname != NULL ? fname:"(null)");
    printf("\n");
    system("pause");
    return 0;
    }
    The program outputs:

    path: "C:/Users/Michael/Desktop/File Directory/example.doc"
    filename: "example.doc"


    What I'd like to add is a part where it would output

    path:
    filename:
    extension: ".doc"

    Is this possible using the code that I have set up? Thanks in advance for any help.

    Michael

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Use strrchr to get a pointer to the last occuring '.' and then return pointer+1 as the extension.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    19

    Parsing question

    Thanks a lot for your help. Would I simply add the 'strrchr' pointer like I have below?

    Code:
    char *fname = NULL;
         if (pathname)
              {
               fname = strrchr (pathname, '/')+1;
               fname = strrchr (pathname, '.')+1;
              }
    return fname;
    }

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    I modified your program into the following.Try with that.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    char *exten;
    char *file_from_path(char *pathname)
    {
            char *fname = NULL;
            int diff;
            char *file=strrchr(pathname,'/')+1;
            exten=strrchr(file,'.');
            diff=exten-file;
            fname=(char *)malloc(diff);
    
            if (pathname)
            {
                    strncpy(fname,file,diff);
            }
          return fname;
    }
    
    int main (void)
    {
    
            char pathname[] = "C:/Users/Michael/Desktop/File Directory/example.doc";
            char *fname = file_from_path(pathname);
            printf("path: \"%s\"\nfilename: \"%s\"\n", pathname, fname != NULL ? fname:"(null)");
            printf("extension:%s\n",exten);
            printf("\n");
            system("pause");
            return 0;
    }

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You'd want to check for NULL before adding anything to it. What if strrchr() didn't find a full-stop? You'll most likely segfault when you try access NULL+1.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Thought this code must be a simple one,
    It has no undefined behaviour even the path name has no extension

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    char *exten;
    char *file_from_path(char *pathname)
    {
            char *fname = NULL;
            int diff;
            char *file=strrchr(pathname,'/')+1;
          char *dif ;
            if ( exten=strrchr(file,'.') )
            {
            dif=exten ;
            exten++;    // Move one step from the null 
            file[dif-file]='\0';    
            }
    
            return file ;
    }
    
    int main (void)
    {
    
            char pathname[] = "C:/Users/Michael/Desktop/File Directory/example.doc";
            char *fname = file_from_path(pathname);
            printf("path: \"%s\"\nfilename: \"%s\"\n", pathname, fname != NULL ? fname:"(null)");
            printf("extension:%s\n",exten);
            printf("\n");
     system("pause");
            return 0;
    }

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: Question on parsing

    If I pass NULL or some other value to file_from_path() function it will give segmentation fault.

    For that need to check the strrchr return value before using that value.

    Code:
    char *file=strrchr(pathname,'/')+1;
    It may return NULL also. so after checking that return value we can use that for our purpose.

    Try this code. It works fine.

    Code:
    char *exten;
    char *file_from_path(char *pathname)
    {
            char *fname = NULL, *file=NULL,*tmp;
            int diff;
    
            tmp=strrchr(pathname,'/');
            if(tmp)
            file=tmp+1;
    
          char *dif ;
    
            if(file && (exten=strrchr(file,'.')) )
            {
            dif=exten ;
            exten++;    // Move one step from the null
            file[dif-file]='\0';
            }
    
            return file ;
    }
    
    int main (void)
    {
    
            char pathname[] = "hai";
            char *fname = file_from_path(pathname);
            printf("path: \"%s\"\nfilename: \"%s\"\n", pathname, fname != NULL ? fname:"(null)");
            printf("extension:%s\n",exten);
            printf("\n");
     system("pause");
            return 0;
    }
    Last edited by sganesh; 03-01-2010 at 12:34 AM. Reason: change the code

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Alexander jack View Post
    Thought this code must be a simple one,
    It has no undefined behaviour even the path name has no extension

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    char *exten;
    char *file_from_path(char *pathname)
    {
            char *fname = NULL;
            int diff;
            char *file=strrchr(pathname,'/')+1;
          char *dif ;
            if ( exten=strrchr(file,'.') )
            {
            dif=exten ;
            exten++;    // Move one step from the null 
            file[dif-file]='\0';    
            }
    
            return file ;
    }
    
    int main (void)
    {
    
            char pathname[] = "C:/Users/Michael/Desktop/File Directory/example.doc";
            char *fname = file_from_path(pathname);
            printf("path: \"%s\"\nfilename: \"%s\"\n", pathname, fname != NULL ? fname:"(null)");
            printf("extension:%s\n",exten);
            printf("\n");
     system("pause");
            return 0;
    }
    It is undefined. NULL is used in arithmetic. There still might be no slashes in the path.

    Seriously, all the above "solutions" are hideous. There's nothing wrong with:
    Code:
    char * file = NULL;
    /* ... */
    file = strrchr(pathname, '/');
    if(file)
       file++;
    As for calling strrchr() twice for 'error checking'... not so sure about that.
    Last edited by zacs7; 02-28-2010 at 11:54 PM.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    57
    Quote Originally Posted by zacs7 View Post
    It is undefined. NULL is used in arithmetic. There still might be no slashes in the path.

    Seriously, all the above "solutions" are hideous. There's nothing wrong with:
    Code:
    char * file = NULL;
    /* ... */
    file = strrchr(pathname, '/');
    if(file)
       file++;
    As for calling strrchr() twice for 'error checking'... not so sure about that.
    Yeah. For checking the error we shouldn't call the function twice.

    we can use some temporary variable or "file" variable to store and check that value.

    But I just gave the suggestion that without checking return value If we use that value it may give error.

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    19

    Thanks a bunch for helping me with the parsing question!

    Thank you very much for helping me with this problem. You guys are great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  2. Parsing Question
    By DickArmy in forum C Programming
    Replies: 2
    Last Post: 11-01-2009, 12:08 PM
  3. Parsing for Dummies
    By MisterWonderful in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2004, 05:31 PM
  4. question about parsing a string??
    By newbie02 in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2003, 10:28 AM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM