473,662 Members | 2,536 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

get filename from string

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

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:\hel lo.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 13182
Mark wrote:
let's say i have a string "c:\hello.t xt"

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:\hel lo.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*******@gmai l.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.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.t xt" 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(MyStrin g, '.');

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,strl en(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

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

Similar topics

5
4274
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(); return 0; How do I convert the filename string to the const char * that fstream
2
1608
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 = 'test.file."; $i = 0; and I want to change $filename to become 'test.file.0'.
5
1558
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 "Completed" and will skip the code if the test passes. Unfortunately, the string returned to me in a watch shows as "Completed\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" (\0s ad nauseum) and this doesn't match...
0
1197
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: http://abc.com/docs/123.txt return 'Web-Based'
7
7810
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}" As far as I know yet -- hence this question -- there is no 'one solution fits all', but instead there are several parts that have to be put together to check. What I have so far is, and would like as much feedback as possible to ensure I've...
3
1576
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
2803
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 only one word. I try to concatenate each word read into a string variable, separated by a delimiter character. Even though I am able to read the words from the file correctly, I am not able to correctly concatenate the string. I was expecting the...
6
8810
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, thanks,
10
2517
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 names_m.txt or names_f.txt but i don't know until runtime. i tried this but it didnt quite work... char fileName = {"/data/names_"};
0
2130
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: retv=mylist .... return retv
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8764
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.