Results 1 to 9 of 9

Thread: getting file extention from filename? split?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    8

    getting file extention from filename? split?

    I have written perl and VB and this is my very first attept at C

    there is a variable called name
    which is a filename like somefile.ext

    i want to check that name does not have the extention .mp3 or .rar or .ace (make it easy for me to add new types)
    and if it is one of those extensions then it should do this

    addreply(553, MSG_BAD_FILE_TYPE, name);
    goto end;

    a way to get whatever letters are after the last dot into a string would be useful
    Eric

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    What type is the VAR you are holding the string in?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    8
    im not sure
    its a part of this function is that what you mean?

    void dostor(char *name, const int append, const int autorename)

    its name that contains the filename
    Eric

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Well I am not going to write the whole thing for you but you can use some code I have used before to get the right part of a string. Just send it your sting and 4 for the length, then you can look the returned value and see if it has the extentions you are looking for

    PHP Code:
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
    //| Function:    *right()
    //| In:            const char *chString
    //|                    In Char Or String Of Chars
    //|                unsigned int len
    //|                    Length To Trim
    //| Return:        char*
    //| Notes:        This Function Trims Chars From Right To Left  <---
    //| Example:    char p[] = "TTTT1234";
    //|                LPCTSTR t;
    //|                t = right(p,3);        //Should Return "234"
    /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

    char *right(const char *chString,unsigned int len)
    {
    /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
        
    unsigned int i;                //Loop VAR
        
    int knIndex;                //More Loop VARs
        
    char *buf;                    //Temp String VAR
    /*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/

        
    if (!strlen(chString))                        //If There Is No String
            
    return NULL;                            //Return NULL

        
    if (len strlen(chString))                    //If The Amount To Copy Is Larger Than The String
            
    len strlen(chString);                    //Set The Length To The Length Of The String

        
    buf = (char *)malloc(len 1);

        
    //Copy The Part We Want From The Orginal String To The Result
        
    for (0nIndex = ((strlen(chString))-len), 0leni++)    
        {        
            if (
    chString[i] != '\0')            //If Not A NULL Char        
                
    buf[k++] = chString[nIndex++];
            else
                break;
        }

        
    buf[k] = '\0';                        //Add A NULL Char To The End Of The String

        
    return buf;


    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What if your extension is more or less than 3 characters?

    If you're on Windows:
    splitpath, _wsplitpathSee Also
    File Handling Routines | _fullpath | _getmbcp | _makepath | _setmbcp
    Requirements
    Routine Required header Compatibility
    _splitpath <stdlib.h> Win 98, Win Me, Win NT, Win 2000, Win XP
    _wsplitpath <stdlib.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP

    For additional compatibility information, see Compatibility in the Introduction.

    Libraries

    All versions of the C run-time libraries.
    Break a path name into components.

    void _splitpath(
    const char *path,
    char *drive,
    char *dir,
    char *fname,
    char *ext
    );
    void _wsplitpath(
    const wchar_t *path,
    wchar_t *drive,
    wchar_t *dir,
    wchar_t *fname,
    wchar_t *ext
    );
    Parameters
    path
    Full path
    drive
    Optional drive letter, followed by a colon (
    dir
    Optional directory path, including trailing slash. Forward slashes ( / ), backslashes ( \ ), or both may be used.
    fname
    Base filename (no extension)
    ext
    Optional filename extension, including leading period (.)
    Remarks
    The _splitpath function breaks a path into its four components. _splitpath automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the multibyte code page currently in use. _wsplitpath is a wide-character version of _splitpath; the arguments to _wsplitpath are wide-character strings. These functions behave identically otherwise.

    Generic-Text Routine Mappings

    TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
    _tsplitpath _splitpath _splitpath _wsplitpath

    Each argument is stored in a buffer; the manifest constants _MAX_DRIVE, _MAX_DIR, _MAX_FNAME, and _MAX_EXT (defined in STDLIB.H) specify the maximum size necessary for each buffer. The other arguments point to buffers used to store the path elements. After a call to _splitpath is executed, these arguments contain empty strings for components not found in path. You can pass a NULL pointer to _splitpath for any component you don't need.

    Requirements
    Routine Required header Compatibility
    _splitpath <stdlib.h> Win 98, Win Me, Win NT, Win 2000, Win XP
    _wsplitpath <stdlib.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP

    For additional compatibility information, see Compatibility in the Introduction.

    Libraries

    All versions of the C run-time libraries.

    Example
    /* MAKEPATH.C */

    #include <stdlib.h>
    #include <stdio.h>

    void main( void )
    {
    char path_buffer[_MAX_PATH];
    char drive[_MAX_DRIVE];
    char dir[_MAX_DIR];
    char fname[_MAX_FNAME];
    char ext[_MAX_EXT];

    _makepath( path_buffer, "c", "\\sample\\crt\\", "makepath", "c" );
    printf( "Path created with _makepath: %s\n\n", path_buffer );
    _splitpath( path_buffer, drive, dir, fname, ext );
    printf( "Path extracted with _splitpath:\n" );
    printf( " Drive: %s\n", drive );
    printf( " Dir: %s\n", dir );
    printf( " Filename: %s\n", fname );
    printf( " Ext: %s\n", ext );
    }

    Output
    Path created with _makepath: c:\sample\crt\makepath.c

    Path extracted with _splitpath:
    Drive: c:
    Dir: \sample\crt\
    Filename: makepath
    Ext: .c

    See Also
    File Handling Routines | _fullpath | _getmbcp | _makepath | _setmbcp
    Not tried that, so not sure how long _MAX_EXT is...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    8
    its running on linux
    the above doesnt work i get errors:

    ftpd.c: In function `dostor':
    ftpd.c:3327: parse error before `char'
    ftpd.c:3335: `chString' undeclared (first use in this function)
    ftpd.c:3335: (Each undeclared identifier is reported only once
    ftpd.c:3335: for each function it appears in.)
    ftpd.c:3336: warning: `return' with a value, in function returning void
    ftpd.c:3338: `len' undeclared (first use in this function)
    ftpd.c:3344: `i' undeclared (first use in this function)
    ftpd.c:3344: `nIndex' undeclared (first use in this function)
    ftpd.c:3344: `k' undeclared (first use in this function)
    ftpd.c:3346: empty character constant
    ftpd.c:3352: empty character constant
    ftpd.c:3354: warning: `return' with a value, in function returning void
    ftpd.c:3253: label `end' used but not defined
    ftpd.c: At top level:
    ftpd.c:3360: parse error before `if'
    ftpd.c:3377: parse error before `553'
    they go on...


    btw does anyone know why this wont work?
    upload.pl is a shell script and will output either 1 or 0

    if (system("/root/upload.pl") != 1) {
    addreply(553, MSG_SANITY_FILE_FAILURE, name);
    goto end;
    }
    Eric

  7. #7
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    What I gave you was another function and it looks like you are trying to put it inside of ANOTHER function. You cant do that.

    Since you are just now telling us you are using Linux, Parksie's example will not work (I dont think).

    I think you need to spend sometime learning more basic stuff....to be blunt because you really do seem to know what you are doing. Saying that, unless someone is really nice, they are not going to write this for you.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  8. #8
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    PHP Code:

    char
    getExt( const charfilenamecharextbuffer )
    {
        
    // the dot
        
    const char dot '.'

        
    // counter, position
        
    int n = -1pos 0;

        
    // empty extension buffer
        
    extbuffer[0] = NULL;

        
    // Get the position of last dot
        
    while (filename[++n])
        {
            if (
    filename[n] == dot)
            {
                
    // last position will overide this
                
    pos n;
            }
        }

        
    // if no dot found, return null (.txt is invalid)
        
    if (!pos)
        {
            return 
    extbuffer;
        }

        
    // the length of the string
        
    int len n;

        
    // terminate the extension string with null
        
    extbuffer[len - ++pos] = NULL;

        
    // Now get everything from the dot to the end
        
    while (filename[--n] != dot)
        {
            
    extbuffer[len - ++pos] = filename[n];
        }

        
    // Return the extention
        
    return extbuffer;

    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

  9. #9
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    use it like:

    Code:
    if (!strncmp(getExt(filename, buff), "htm", 3)) {
     // htm or html
    }
    else
    {
      // bla
    }
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width