Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > work with char[]

Reply
Thread Tools

work with char[]

 
 
Marchello
Guest
Posts: n/a
 
      12-06-2005
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.


 
Reply With Quote
 
 
 
 
Bob Hairgrove
Guest
Posts: n/a
 
      12-06-2005
On Tue, 6 Dec 2005 14:42:27 +0200, "Marchello"
<(E-Mail Removed)-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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
deane_gavin@hotmail.com
Guest
Posts: n/a
 
      12-06-2005

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

 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      12-06-2005

"Marchello" <(E-Mail Removed)-kpi.kiev.ua> wrote in message
news:dn40vd$a6p$(E-Mail Removed)-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 <(E-Mail Removed)>


 
Reply With Quote
 
Sumit Rajan
Guest
Posts: n/a
 
      12-06-2005

"Sumit Rajan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Marchello" <(E-Mail Removed)-kpi.kiev.ua> wrote in message
> news:dn40vd$a6p$(E-Mail Removed)-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 <(E-Mail Removed)>
>
>



 
Reply With Quote
 
Marchello
Guest
Posts: n/a
 
      12-06-2005
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE;Kontki if you delete kontiki any program you loaded with it in it 'will not work I have tried it with three programs and none work anymore (if you se it just stop download) 1-Twitch Computer Support 5 04-23-2009 02:45 PM
MS work around on text wrapping in a datagrid does not work TB ASP .Net 2 02-22-2006 10:34 PM
Hi I am new to asp i can not get it to work on xp pro sp2 even though the localhost work but asp pages dont so can some one help craig dicker ASP .Net 9 07-07-2005 11:52 AM
Re: Those cute little "WORK-SAFE" / "NOT WORK-SAFE" tags that people put in the Subject headers of their posts... Soapy Digital Photography 1 08-16-2004 12:07 PM
Re: Those cute little "WORK-SAFE" / "NOT WORK-SAFE" tags that people put in the Subject headers of their posts... Soapy Digital Photography 1 08-16-2004 06:24 AM



Advertisments