473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

work with char[]

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.
Dec 6 '05 #1
5 2935
On Tue, 6 Dec 2005 14:42:27 +0200, "Marchello"
<ma*******@rtf-15.ntu-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
No**********@Home.com
Dec 6 '05 #2

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 '05 #3

"Marchello" <ma*******@rtf-15.ntu-kpi.kiev.ua> wrote in message
news:dn**********@news.ntu-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 <su****@msdc.hcltech.com>
Dec 6 '05 #4

"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3v*************@individual.net...

"Marchello" <ma*******@rtf-15.ntu-kpi.kiev.ua> wrote in message
news:dn**********@news.ntu-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 <su****@msdc.hcltech.com>

Dec 6 '05 #5
> 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.
Dec 6 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sabrina | last post by:
Can someone help? I have been trying to get the hash_map in C++ for .NET to work with strings and const char*. I am using the const char* as the key and a pointer to another class as the data...
1
by: ayiiq180 | last post by:
my hook already in a dll and the handle is shared,but the hook cant work well,when i run the application,My mouse click the application's view,the hook work well,but when i click the other...
4
by: Nan Li | last post by:
Hello, Can any one get the 'copy' statement below to work? I want it to do the same thing as the 'for' loop does. But I got a lot of STL error during compile. Thanks, Nan #include <map>
15
by: Eirik | last post by:
This is a little function I wrote, inspired by the thread "Urgent HELP! required for Caesar Cipher PLEASE" $ cat /home/keisar/bin/c/ymse/rot13.h char rot13(char character) { int changed;...
6
by: dddddddd2444444 | last post by:
Hi,please help... It works fine when I define a 2-D array like char code. But it won't work when I try to define the array dynamically using a function. It just crashes. Does anyone know why?...
4
by: Tony Johansson | last post by:
Hello!! I have done some operator overloading but my main testprogram doesn't work well. Have you any idea which of my methods are wrong? #include <iostream> #include <string> using...
5
by: Matt Kowalczyk | last post by:
How come this function doesn't work? void shift(char** list, int index) { char buff = (*list); memmove((*list)+1, *list, index); (*list) = buff; return; } I call it like so:
14
by: jagguy | last post by:
this works char *p ; p="xat"; cout << p <<endl;
5
by: jerry | last post by:
I need to modify the code of a command-line tool. The source code starts out like this: int main(int argc, char *argv) { int ch, A, B ; while ((ch = getopt(argc, argv, "AB")) != -1)...
23
by: Martin T. | last post by:
Hi all! char* p = new char; // POD! .... delete p; // std compliant delete p; // will work on VC8 free(p); // will also work on VC8 I am interested on which platforms/compilers the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.