How can I split a filename into a filename string and extension string?

This is a discussion on How can I split a filename into a filename string and extension string? within the C Programming forums, part of the General Programming Boards category; Let's say we have one string containg "nethack.cnf". How can I seperate it into two strings? (one containg the filename ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    6

    How can I split a filename into a filename string and extension string?

    Let's say we have one string containg "nethack.cnf". How can I seperate it into two strings? (one containg the filename "nethack" and the other containing the extension, "cnf")

    Also, any algorithms that do what I mention above plus remove pathnames would be a plus

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    sscanf, for one.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    TCB
    TCB is offline
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    Quote Originally Posted by sofakng
    Let's say we have one string containg "nethack.cnf". How can I seperate it into two strings? (one containg the filename "nethack" and the other containing the extension, "cnf")

    Also, any algorithms that do what I mention above plus remove pathnames would be a plus
    I don't know the code, but can't you read the string until char = '.' - 1 (one char short of the . char)? Maybe use strcpy() and write that to a buffer of sorts, and there's your first string. Then do the same from '\0' back to the '.' and strcpy() that to another buffer.

    In VB you can remove a certain number of chars from either end of the string; but I don't know C well enough to know if there's a function to do that in this language.

    TB

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    One way would be to write one yourself. It's not hard. Just loop through each character and decide which to dump and which to keep.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strrchr


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    strtok

    strtok

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Actually, when I did this, I wrote one myself. (Though Quzah's suggestion of strrchr is a good one.) Basically, you need to find the last '.'. strrchr() will do that. You don't really want to search forward, should they pass something exotic like I.Am.Special, and end up with ".Am.Special". There's also the issue of them passing a extensionless file that is in a directory with an extension, such as "~/.nethack/afile", where the dot in that is part of a directory. (Or something like "../nethack/file")
    So, combining all that: Use strrchr() to search backwards for a . - but perhaps (if your situation requires it) do another search for '/' or '\' to ensure that you're only getting the one in the filename. (If you don't have a directory, then you wouldn't need to worry.)

    Just my 2cent.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using string operations to save part of a file path
    By JackR in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2007, 04:48 PM
  2. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21