work with char[]

M

Marchello

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.
 
B

Bob Hairgrove

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 .
 
D

deane_gavin

Marchello said:
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 said:
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
 
S

Sumit Rajan

Marchello said:
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.
 
S

Sumit Rajan

Sumit Rajan said:
Marchello said:
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)
 
M

Marchello

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top