473,839 Members | 1,328 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::fi nd() function always returns std::string::np os (-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<TCH AR> configString(bu fLen);
//populate configString... .
CleanCfgFile(re interpret_cast< std::string*>(& configString));
//...
}

bool CleanCfgFile(st d::string *cfgFile)
{
TCHAR XMLHeader[] = _T("<?xml");
std::string::si ze_type start = cfgFile->find(XMLHeader );
//start == std::string::np os!
//...
}

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 4615
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::fi nd() function always returns std::string::np os (-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<TCH AR> configString(bu fLen);
//populate configString... .
CleanCfgFile(re interpret_cast< std::string*>(& configString));
//...
}

bool CleanCfgFile(st d::string *cfgFile)
{
TCHAR XMLHeader[] = _T("<?xml");
std::string::si ze_type start = cfgFile->find(XMLHeader );
//start == std::string::np os!
//...
}

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_cas t<> to cast
between a std::string* and a std::vector<cha r> 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<cha r>.

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::fi nd() function always returns std::string::np os (-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<TCH AR> configString(bu fLen);
//populate configString... .
CleanCfgFile(re interpret_cast< std::string*>(& configString));
//...
}

bool CleanCfgFile(st d::string *cfgFile)
{
TCHAR XMLHeader[] = _T("<?xml");
What is TCHAR? What is _T()?
std::string::si ze_type start = cfgFile->find(XMLHeader );
//start == std::string::np os!
//...
}

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_cas t used in this way invokes undefined behaviour.

Try it this way:

CleanCfgFile(st d::string(confi gString.begin() , configString.en d()));

bool CleanCfgFile(co nst 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_cas t<> to cast
between a std::string* and a std::vector<cha r> has behavior so
Oops - I meant "between a std::string* and a std::vector<cha r>* 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::fi nd() function always returns std::string::np os (-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<TCH AR> configString(bu fLen);
//populate configString... .
CleanCfgFile(re interpret_cast< std::string*>(& configString));
//...
}

bool CleanCfgFile(st d::string *cfgFile)
{
TCHAR XMLHeader[] = _T("<?xml");
std::string::si ze_type start = cfgFile->find(XMLHeader );
//start == std::string::np os!
//...
}

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_cas t<> to cast
between a std::string* and a std::vector<cha r> 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<cha r>.

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::fi nd() function always returns std::string::np os (-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<TCH AR> configString(bu fLen);
//populate configString... .
CleanCfgFile(re interpret_cast< std::string*>(& configString));
//...
}

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


What is TCHAR? What is _T()?
std::string::si ze_type start = cfgFile->find(XMLHeader );
//start == std::string::np os!
//...
}

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_cas t used in this way invokes undefined behaviour.

Try it this way:

CleanCfgFile(st d::string(confi gString.begin() , configString.en d()));

bool CleanCfgFile(co nst 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
2069
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 presented with a form that has a textarea. If they type in words and then hit submit, then all is fine. But if they write their entry in WordPerfect or Microsoft Word or some such, and copy and paste it, then they might be bringing strange...
5
8763
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) throw(std::runtime_error)
6
7096
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 file, how to do ? thx
32
14933
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
8
6213
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& number, NumberFormatInfo info, Boolean parseDecimal) +2178925" This is beacuse some of my TextBoxes are empty (no value). If I enter a value it is OK. I need to use Null values to indicate that the field is not fileld in.
11
5365
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 (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
1
1427
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 currently use a single sign on to our oracle db for our client/server VB6 apps. We currently have a DLL that the VB6 apps call to get the production connection string. What this afforded us was the ability to change the production db password for all
7
7826
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...
14
4102
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
9697
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
10908
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10649
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
9426
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...
1
7829
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
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...
1
4487
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 we have to send another system
2
4064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
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.