work with char[]

Discussion in 'C++' started by Marchello, Dec 6, 2005.

  1. Marchello

    Marchello Guest

    Hi All.
    I need to extract file's name from a full-path file's name string.
    For instance:

    char full_name[256] = {'\0'};
    char name[256] = {'\0'};

    sprintf(full_name, "C:\\folder\\file.ext");

    And I need something like this:

    ExtractFileName(full_name, name);
    // now 'name' must has value "file.ext"

    How to write such func ExtractFileName(const char *full, char *dest_name) ?

    Sorry for my english.
     
    Marchello, Dec 6, 2005
    #1
    1. Advertising

  2. On Tue, 6 Dec 2005 14:42:27 +0200, "Marchello"
    <-kpi.kiev.ua> wrote:

    >Hi All.
    >I need to extract file's name from a full-path file's name string.
    >For instance:
    >
    >char full_name[256] = {'\0'};
    >char name[256] = {'\0'};
    >
    >sprintf(full_name, "C:\\folder\\file.ext");
    >
    >And I need something like this:
    >
    >ExtractFileName(full_name, name);
    > // now 'name' must has value "file.ext"
    >
    >How to write such func ExtractFileName(const char *full, char *dest_name) ?
    >
    >Sorry for my english.
    >


    std::string has many member funcions which are well suited to this
    task, for example rfind() and substr(). I suggest you familiarize
    yourself with this class ASAP (and forget about using char* and all
    the problems which come with that).

    Also, there is the Boost tokenizer library at http://www.boost.org .

    --
    Bob Hairgrove
     
    Bob Hairgrove, Dec 6, 2005
    #2
    1. Advertising

  3. Marchello

    Guest

    Marchello wrote:
    > Hi All.
    > I need to extract file's name from a full-path file's name string.
    > For instance:
    >
    > char full_name[256] = {'\0'};
    > char name[256] = {'\0'};


    This is a C++ newsgroup, so

    std::string full_name;
    std::string name;

    You'll need to #include <string>

    > sprintf(full_name, "C:\\folder\\file.ext");


    full_name = "C:\\folder\\file.ext";

    > And I need something like this:
    >
    > ExtractFileName(full_name, name);
    > // now 'name' must has value "file.ext"
    >
    > How to write such func ExtractFileName(const char *full, char *dest_name) ?


    Make it

    std::string ExtractFileName(const std::string& full)
    {
    // do the work of extracting just the file name from the whole path
    // to do that, look up the find_last_of and substr member functions
    of std::string

    // return the file name string
    }

    Gavin Deane
     
    , Dec 6, 2005
    #3
  4. Marchello

    Sumit Rajan Guest

    "Marchello" <-kpi.kiev.ua> wrote in message
    news:dn40vd$a6p$-kpi.kiev.ua...
    > Hi All.
    > I need to extract file's name from a full-path file's name string.
    > For instance:
    >
    > char full_name[256] = {'\0'};
    > char name[256] = {'\0'};
    >
    > sprintf(full_name, "C:\\folder\\file.ext");
    >
    > And I need something like this:
    >
    > ExtractFileName(full_name, name);
    > // now 'name' must has value "file.ext"
    >
    > How to write such func ExtractFileName(const char *full, char *dest_name)
    > ?



    Why don't you use std::string? You could try something like:
    std::string ExtractFileName(const std::string& full)

    {

    std::string::size_type idx = full.find_last_of("\\");

    if (idx == std::string::npos)

    return full.substr(idx+1);

    else

    return ""; //do what you need to do to handle this case

    }



    Regards,

    Sumit.
    --
    Sumit Rajan <>
     
    Sumit Rajan, Dec 6, 2005
    #4
  5. Marchello

    Sumit Rajan Guest

    "Sumit Rajan" <> wrote in message
    news:...
    >
    > "Marchello" <-kpi.kiev.ua> wrote in message
    > news:dn40vd$a6p$-kpi.kiev.ua...
    >> Hi All.
    >> I need to extract file's name from a full-path file's name string.
    >> For instance:
    >>
    >> char full_name[256] = {'\0'};
    >> char name[256] = {'\0'};
    >>
    >> sprintf(full_name, "C:\\folder\\file.ext");
    >>
    >> And I need something like this:
    >>
    >> ExtractFileName(full_name, name);
    >> // now 'name' must has value "file.ext"
    >>
    >> How to write such func ExtractFileName(const char *full, char *dest_name)
    >> ?

    >
    >
    > Why don't you use std::string? You could try something like:
    > std::string ExtractFileName(const std::string& full)
    >
    > {
    >
    > std::string::size_type idx = full.find_last_of("\\");
    >
    > if (idx == std::string::npos)


    Typo above. Should be:

    if (idx != std::string::npos)





    > return full.substr(idx+1);
    >
    > else
    >
    > return ""; //do what you need to do to handle this case
    >
    > }
    >
    >
    >
    > Regards,
    >
    > Sumit.
    > --
    > Sumit Rajan <>
    >
    >
     
    Sumit Rajan, Dec 6, 2005
    #5
  6. Marchello

    Marchello Guest

    > std::string ExtractFileName(const std::string& full)
    > {
    > std::string::size_type idx = full.find_last_of("\\");
    > if (idx != std::string::npos)
    > return full.substr(idx+1);
    > else return ""; }


    Thanks. This code is work.
     
    Marchello, Dec 6, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,520
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    480
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    589
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,060
    Ian Collins
    May 9, 2006
Loading...

Share This Page