473,387 Members | 1,683 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,387 software developers and data experts.

get filename from string

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?

Jan 20 '06 #1
10 13158
Mark wrote:
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.

--
Ian Collins.
Jan 20 '06 #2
thanks. the cstring functions aren't very helpful.

Jan 20 '06 #3
Mark wrote:
thanks. the cstring functions aren't very helpful.


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

Jan 20 '06 #4

Shark wrote:
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?

Jan 20 '06 #5

Mark wrote:
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

Jan 20 '06 #6
Mark wrote:
Shark wrote:
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?


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

Jan 20 '06 #7
"Mark" <mn*******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

Shark wrote:
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?


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.
Jan 21 '06 #8
Gavin Deane wrote:
Mark wrote:
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 :-)


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

Jan 22 '06 #9

Shark wrote:
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.

Jan 22 '06 #10

Jim Langston wrote:
"Mark" <mn*******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...

Shark wrote:
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?


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.

Jan 22 '06 #11

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

Similar topics

5
by: DellBoy | last post by:
I would like to be able to do something like this: string sFileInfo = "MyFile.tmp"; fstream InfoFile; InfoFile.open(sFileInfo, fstream::app); // >> do some i/o operations << InfoFile.close();...
2
by: Robizzle | last post by:
I am having problems appending a number to the end of a string. I searched google and google forums and couldn't figure it out .... sorry i'm sure its simple. so I have: $filename =...
5
by: Claire | last post by:
I read a list of name/value pairs from a remote (pocket pc + RAPI) registry into a string array (3rd party wrapper for rapi registry objects). As I check through each set, I test each name against...
0
by: ABC | last post by:
Which function can determine the URI is web-based or File-based filename string? I want a function can determome the file with path is web or file-system-based filename, for example: ...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
3
by: Fir5tSight | last post by:
Hi All, I have an interface class defined as follows: class FileName : IComparable { public FileName(string fileName, string packageName) { // // TODO: Add constructor logic here
5
by: Generic Usenet Account | last post by:
I have been to recreate a problem that I am having with strings with the trivial code snippet given below. In the trivial code example, I am reading five lines from a data file, each line having...
6
by: Adriano | last post by:
Can anyone recommend a simple way to compress/decomress a String in .NET 1.1 ? I have a random string of 70 characters, the output from a DES3 encryption, and I wish to reduce the lengh of it, ...
10
by: drsmooth | last post by:
i need to open a file whose name i only know part of until runtime. it is based on a char value. the char represents gender and is either 'm' or ''f' i need to load from a text file called...
0
by: menosaint | last post by:
hi all i want to check a condition and if true should return a filename string from a list.if the condition is false i am returning a "" (string literal).. retv="" if somecondition:...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.