473,513 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stl string find issue

I have a function that is stripping off some XML from a configuration
file. But, when I do a search for the pieces I want to strip, the
std::string::find() function always returns std::string::npos (-1).

I can print out the config string at the beginning of CleanCfgFile(),
and the strings are there exactly the way I'm looking for them. So, the
question is, what am I doing wrong?

Function()
{
std::vector<TCHAR> configString(bufLen);
//populate configString....
CleanCfgFile(reinterpret_cast<std::string*>(&confi gString));
//...
}

bool CleanCfgFile(std::string *cfgFile)
{
TCHAR XMLHeader[] = _T("<?xml");
std::string::size_type start = cfgFile->find(XMLHeader);
//start == std::string::npos!
//...
}

Why am I using a vector and not a string in Function(), you ask? It's
just easier for the rest of the program. I could probably change it if
that's the problem... I just thought you could cast between the two and
be fine.

Thanks,
PaulH

Jun 30 '06 #1
5 4599
PaulH wrote:
I have a function that is stripping off some XML from a configuration
file. But, when I do a search for the pieces I want to strip, the
std::string::find() function always returns std::string::npos (-1).

I can print out the config string at the beginning of CleanCfgFile(),
and the strings are there exactly the way I'm looking for them. So, the
question is, what am I doing wrong?

Function()
{
std::vector<TCHAR> configString(bufLen);
//populate configString....
CleanCfgFile(reinterpret_cast<std::string*>(&confi gString));
//...
}

bool CleanCfgFile(std::string *cfgFile)
{
TCHAR XMLHeader[] = _T("<?xml");
std::string::size_type start = cfgFile->find(XMLHeader);
//start == std::string::npos!
//...
}

Why am I using a vector and not a string in Function(), you ask? It's
just easier for the rest of the program. I could probably change it if
that's the problem... I just thought you could cast between the two and
be fine.


You've answered your own question. Assuming for the moment that a
"TCHAR" is just a typedef for a char, using reinterpret_cast<> to cast
between a std::string* and a std::vector<char> has behavior so
undefined, it's hard to know where to begin. But here's a hint - how
std::string is implemented is implementation defined. If you have a
copy of Scott Meyers' Effective STL, take a look at Item 15, where he
discusses several different common implementations of std::string.
There is certainly no guarantee - it's not even particularly likely -
that std::string shares a common internal layout and implementation
with std::vector<char>.

Best regards,

Tom

Jun 30 '06 #2
PaulH schrieb:
I have a function that is stripping off some XML from a configuration
file. But, when I do a search for the pieces I want to strip, the
std::string::find() function always returns std::string::npos (-1).

I can print out the config string at the beginning of CleanCfgFile(),
and the strings are there exactly the way I'm looking for them. So, the
question is, what am I doing wrong?

Function()
{
std::vector<TCHAR> configString(bufLen);
//populate configString....
CleanCfgFile(reinterpret_cast<std::string*>(&confi gString));
//...
}

bool CleanCfgFile(std::string *cfgFile)
{
TCHAR XMLHeader[] = _T("<?xml");
What is TCHAR? What is _T()?
std::string::size_type start = cfgFile->find(XMLHeader);
//start == std::string::npos!
//...
}

Why am I using a vector and not a string in Function(), you ask? It's
just easier for the rest of the program. I could probably change it if
that's the problem... I just thought you could cast between the two and
be fine.


What makes you think that you can cast between them? You cannot. IMHO,
reinterpret_cast used in this way invokes undefined behaviour.

Try it this way:

CleanCfgFile(std::string(configString.begin(), configString.end()));

bool CleanCfgFile(const std::string& cfgFile)
{
// ...
}

Thomas
Jun 30 '06 #3
Thomas Tutone wrote:

<snip>
You've answered your own question. Assuming for the moment that a
"TCHAR" is just a typedef for a char, using reinterpret_cast<> to cast
between a std::string* and a std::vector<char> has behavior so
Oops - I meant "between a std::string* and a std::vector<char>* has
behavior so"
undefined, it's hard to know where to begin.


<snip>

Best regards,

Tom

Jun 30 '06 #4
*dusts off Effective C++*
Yup, you're right.
Thanks for the tip.

-PaulH

Thomas Tutone wrote:
PaulH wrote:
I have a function that is stripping off some XML from a configuration
file. But, when I do a search for the pieces I want to strip, the
std::string::find() function always returns std::string::npos (-1).

I can print out the config string at the beginning of CleanCfgFile(),
and the strings are there exactly the way I'm looking for them. So, the
question is, what am I doing wrong?

Function()
{
std::vector<TCHAR> configString(bufLen);
//populate configString....
CleanCfgFile(reinterpret_cast<std::string*>(&confi gString));
//...
}

bool CleanCfgFile(std::string *cfgFile)
{
TCHAR XMLHeader[] = _T("<?xml");
std::string::size_type start = cfgFile->find(XMLHeader);
//start == std::string::npos!
//...
}

Why am I using a vector and not a string in Function(), you ask? It's
just easier for the rest of the program. I could probably change it if
that's the problem... I just thought you could cast between the two and
be fine.


You've answered your own question. Assuming for the moment that a
"TCHAR" is just a typedef for a char, using reinterpret_cast<> to cast
between a std::string* and a std::vector<char> has behavior so
undefined, it's hard to know where to begin. But here's a hint - how
std::string is implemented is implementation defined. If you have a
copy of Scott Meyers' Effective STL, take a look at Item 15, where he
discusses several different common implementations of std::string.
There is certainly no guarantee - it's not even particularly likely -
that std::string shares a common internal layout and implementation
with std::vector<char>.

Best regards,

Tom


Jun 30 '06 #5
Sorry. These are microsoft macros. They're for unicode awareness, and
look something like this:
#ifdef UNICODE
#define _T L
#define TCHAR WCHAR
#else
#define _T
#define TCHAR CHAR
#endif

where you see std::string in my code, it actually says TSTRING, which
is either std::string or std::wstring depending on UNICODE usage. I
just eliminated that for simplicity and forgot about the other ones.

Thanks for your suggestion. It works, but it doesn't change the
contents of the original vector, so I'd have to make a string copy,
send it to the clean function, then copy that back to vector format.
Ugly, but it would work.

Thomas J. Gritzan wrote:
PaulH schrieb:
I have a function that is stripping off some XML from a configuration
file. But, when I do a search for the pieces I want to strip, the
std::string::find() function always returns std::string::npos (-1).

I can print out the config string at the beginning of CleanCfgFile(),
and the strings are there exactly the way I'm looking for them. So, the
question is, what am I doing wrong?

Function()
{
std::vector<TCHAR> configString(bufLen);
//populate configString....
CleanCfgFile(reinterpret_cast<std::string*>(&confi gString));
//...
}

bool CleanCfgFile(std::string *cfgFile)
{
TCHAR XMLHeader[] = _T("<?xml");


What is TCHAR? What is _T()?
std::string::size_type start = cfgFile->find(XMLHeader);
//start == std::string::npos!
//...
}

Why am I using a vector and not a string in Function(), you ask? It's
just easier for the rest of the program. I could probably change it if
that's the problem... I just thought you could cast between the two and
be fine.


What makes you think that you can cast between them? You cannot. IMHO,
reinterpret_cast used in this way invokes undefined behaviour.

Try it this way:

CleanCfgFile(std::string(configString.begin(), configString.end()));

bool CleanCfgFile(const std::string& cfgFile)
{
// ...
}

Thomas


Jun 30 '06 #6

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

Similar topics

2
2053
by: lkrubner | last post by:
What I need to do is find out what characters in a string are not supported by the UTF-8 encoding. The problem arises when someone logs in and uses my php script to create a weblog post. They are...
5
8721
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
6
7079
by: Alan | last post by:
I want to search a string in a file, but I don't know the position, how can I search the string and stop at that line containing that string ? for example, I want to seach the string ".ABC" in a...
32
14762
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
8
6155
by: Ottar | last post by:
I have a few numeric fields, and when I update i get the error: "Input string was not in a correct format". Next line:" System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&...
11
5326
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
1
1403
by: anonymike | last post by:
Hi, I'm having a problem that I've been working with for the last week, I'm not having any luck, I'm hoping someone can help me. Anything would be appreciated. I have a setup where we...
7
7798
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}"...
14
4045
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
0
7259
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,...
0
7380
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
7535
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...
1
7098
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...
0
7523
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...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
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...

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.