#1
  1. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12

    String with a filename


    I have a string with a path to the file, for example:

    Code:
    char filename[MAX_FNAME] = "C:/test/test.txt";
    Now I'd like to remove "C:/test/", so I'd only have a filename. Is there a short way to do this?
  2. #2
  3. No Profile Picture
    "Null, The Terminator"
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    11
    Rep Power
    0
    which OS and what compiler is imp.

    i think fnsplit() will do the job. but its only available on very few compilers and that for DOS or Win environ.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    Off the top of my head...

    Code:
    char filename[MAX_FNAME] = "C:/test/test.txt";
    
    char *pFile = strrchr(filename, '/') + 1;
    Last edited by Nem; August 18th, 2003 at 01:12 PM.
  6. #4
  7. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Originally posted by nulldevice83
    which OS and what compiler is imp.

    i think fnsplit() will do the job. but its only available on very few compilers and that for DOS or Win environ.
    I'm using Lcc-Win32.
  8. #5
  9. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    Originally posted by Nem
    Off the top of my head...

    Code:
    char filename[MAX_FNAME] = "C:/test/test.txt";
    
    char *pFile = strrchr(filename, '/') + 1;
    Yes, this finds the first char of a filename, but it's only a CHAR. I need a string... :(
  10. #6
  11. No Profile Picture
    "Null, The Terminator"
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    11
    Rep Power
    0
    see if your compiler supports fnsplit() otherwise u will have to resort to string manipulaton functions to get your job done.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    109
    Rep Power
    12
    A pointer to a char is the same as any char array, they are both strings. If you wanted you could use strcpy and copy the string back to another char array.
    Code:
    char file[MAX_FNAME]
    char filename[MAX_FNAME];
    
    file = "C:/test/test.txt";
    strcpy(filename, strrchr(file, '/') + 1);
  14. #8
  15. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    This works. Thanks for help.

    Code:
    char path[] = "C:/test/test.txt";
    char filename[MAX_FNAME];
    
    strcpy(filename, strrchr(file, '/') + 1);
    
    printf("Filename: %s" filename);
  16. #9
  17. It Doesn't Work!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Slovenia
    Posts
    112
    Rep Power
    12
    And I've got another question:

    I have a file with settings:

    Name="Loser"

    Now I want to copy 'Loser' (without quotes) into an array. Do I have to fseek() to first quote and then fscanf() till last quote or is there another way?
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,216
    Rep Power
    2222
    Originally posted by Loser
    And I've got another question:

    I have a file with settings:

    Name="Loser"

    Now I want to copy 'Loser' (without quotes) into an array. Do I have to fseek() to first quote and then fscanf() till last quote or is there another way?
    Since it's a text file, why not just read the entire line in and then process the string?

IMN logo majestic logo threadwatch logo seochat tools logo