Results 1 to 6 of 6

Thread: String help

  1. #1
    Guest

    Post

    Im getting the directory of where my prog is from argv[0]. It comes like "C:\BLAH\FOOBAR\PASS.EXE", how do i cut it into this, "C:\BLAH\FOOBAR"?

  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 of VAR are you putting the string into? CString, char[?], etc. ?
    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
    Guest
    Its in "char * argv[0]"

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    _splitpath is the function you need:

    _splitpath, _wsplitpath
    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 );

    Routine
    Required Header
    Compatibility



    _splitpath
    <stdlib.h>
    Win 95, Win NT

    _wsplitpath
    <stdlib.h> or <wchar.h>
    Win 95, Win NT





    For additional compatibility information, see Compatibility in the Introduction.

    Libraries



    LIBC.LIB
    Single thread static library, retail version

    LIBCMT.LIB
    Multithread static library, retail version

    MSVCRT.LIB
    Import library for MSVCRT.DLL, retail version





    Return Value

    None

    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.

    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

    File Handling Routines

    See Also _fullpath, _getmbcp, _makepath, _setmbcp
    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

  5. #5
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    This works too:
    [code]
    char *p = argv[0];
    strrev(p);
    char *Result;
    Result = strchr(p, '\\');
    strrev(Result); //contains what you want
    [code]

    I 90% certain that this code can be shorter.
    The important thing is that it works.
    I am become death, the destroyer of worlds.
    mail:vlatkovr@hotmail.com

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  6. #6
    Guest
    Actually, i came up with this...

    Code:
    int a, b, c;
    char *p = strrchr(argv[0], '\\');
    if(p) *p ='\\0';
    a = strlen(argv[0]);
    b = strlen(p);
    c = a - b;
    argv[0][c] = 0;
    char Dir[100], PassDat[100];
    strcpy(Dir, argv[0]);
    strcpy(PassDat, argv[0]);
    strcat(PassDat, "\\PASS.DAT");
    But thanks though.

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