get filename from string

M

Mark

let's say i have a string "c:\hello.txt"

and i just want the "c:\hello" part, or just "hello" (so that i can
append something to the end of the filename)

i can use strrchr("c:\hello.txt", '.') to find the last period.. but
i'm not sure what good a pointer to that character does me.

any help?
 
I

Ian Collins

Mark said:
let's say i have a string "c:\hello.txt"

and i just want the "c:\hello" part, or just "hello" (so that i can
append something to the end of the filename)

i can use strrchr("c:\hello.txt", '.') to find the last period.. but
i'm not sure what good a pointer to that character does me.

any help?
Use std:string, use the find and substr to do what you require.
 
M

Mark

Shark said:
they are useful if you know how to use them. This is a troll in
disguise!!!

well shark,
if that's the case, how might i do what i asked with cstrings?
 
G

Gavin Deane

Mark said:
thanks. the cstring functions aren't very helpful.

Well they can't be that bad otherwise C would have died before C++ was
even invented and we wouldn't be here :)

You can do what you want using the C string library, but as well as
having to think about what you are trying to do with your strings, you
have to understand and worry about issues to do with pointers, arrays
and string lengths. Using the C++ string library, all these issues are
handled for you and you can concentrate on solving your actual problem.
You'll find it a lot quicker to write correct, working code.

Gavin Deane
 
S

Shark

Mark said:
well shark,
if that's the case, how might i do what i asked with cstrings?

Sure, but did you just accomplish your goal using std::string? Can you
explain how you did that?
 
J

Jim Langston

Mark said:
well shark,
if that's the case, how might i do what i asked with cstrings?

Well, what good does a pointer to a character in a c-style string do you?
If you must absolutely use c-style strings (don't, std::strings are SO much
better) pointer manipulation was something you had to cope with.

Basically, though, strcpy (string copy) takes two pointers. A pointer from
where to copy to, and a pointer from where to copy from. So if you had a
pointer to the . in "c:\hello.txt" you could use strcpy starting from the
't' in txt to get the extention by adding one to your pointer. Something
like:

char MyString[] = "c:\\hello.txt";
char* Period = strrchr(MyString, '.');

char Ext[10] = "";
strcpy( Ext, Period + 1 ); // Adding one to pointer makes it point to 't'
// It will copy to the first null char.

Now, if you wanted the beginning, you would use strncpy (string number copy)
which takes how many characters to copy. Again, 2 pointers. IIRC the parms
are copy to, copy from, number of chars. So you do:

char FileName[100] = "";
strncpy( FileName, MyString, Period - FileName );

This uses pointer math to determine how many characters to copy.

Again though, forget all this and use std::string. It is so freaking easy
to get pointer math wrong or confuse yourself as to what your pointers are
pointing to and wind up with buffer overflows which are a major pain in the
neck to find.
 
L

Luke Meyers

Gavin said:
Well they can't be that bad otherwise C would have died before C++ was
even invented and we wouldn't be here :)

All the same, I'm perfectly happy with indoor plumbing and will not be
digging an outhouse any time soon just because it used to work that way
and it was good enough. :)
You can do what you want using the C string library, but as well as
having to think about what you are trying to do with your strings, you
have to understand and worry about issues to do with pointers, arrays
and string lengths. Using the C++ string library, all these issues are
handled for you and you can concentrate on solving your actual problem.
You'll find it a lot quicker to write correct, working code.

I say thee, yea! Can I get an "Amen?"

Luke
 
M

Mark

Shark said:
Sure, but did you just accomplish your goal using std::string? Can you
explain how you did that?

string ext = string(argv[1]).substr(strlen(argv[1])-3,
strlen(argv[1]));
string filename = string(argv[1]).substr(0,strlen(argv[1])-4);

'course this won't work if the file extensions are not 3 characters...
shouldn't be a problem for what i'm doing though.
 
M

Mark

Jim said:
Mark said:
well shark,
if that's the case, how might i do what i asked with cstrings?

Well, what good does a pointer to a character in a c-style string do you?
If you must absolutely use c-style strings (don't, std::strings are SO much
better) pointer manipulation was something you had to cope with.

Basically, though, strcpy (string copy) takes two pointers. A pointer from
where to copy to, and a pointer from where to copy from. So if you had a
pointer to the . in "c:\hello.txt" you could use strcpy starting from the
't' in txt to get the extention by adding one to your pointer. Something
like:

char MyString[] = "c:\\hello.txt";
char* Period = strrchr(MyString, '.');

char Ext[10] = "";
strcpy( Ext, Period + 1 ); // Adding one to pointer makes it point to 't'
// It will copy to the first null char.

Now, if you wanted the beginning, you would use strncpy (string number copy)
which takes how many characters to copy. Again, 2 pointers. IIRC the parms
are copy to, copy from, number of chars. So you do:

char FileName[100] = "";
strncpy( FileName, MyString, Period - FileName );

This uses pointer math to determine how many characters to copy.

Again though, forget all this and use std::string. It is so freaking easy
to get pointer math wrong or confuse yourself as to what your pointers are
pointing to and wind up with buffer overflows which are a major pain in the
neck to find.

ah.. right. forgot about pointer math :) thanks for explaining this to
me. good to know nevertheless.
 

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,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top