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

problem with find on stirngs

hi there,
i tried a lot, but it does not work correctly
it is more copied to the substr than expected,
should be only a "<tag>" but outputs "<tag>etc"

shuld parse a xml file like that:
<?xml version="1.0" encoding="iso8859-1"?>
<Hotel id="0" version="0">
<Room id=>
<number>0</number>
<Reservation id=>
<von>01/02/00</von>
<bis>05/41/12</bis>
<GuestA id=>
<vorname>Ruessel</vorname>
<nachname>Schuessel</nachname>
<strasse>AmBach</strasse>

but output is:
<?xml version="1.0" encoding="iso8859-1"?
<Hotel id="0" version="0"
<Room id=>
<number>0<
<Reservation id=>
<von>01/
<bis>05/
<GuestA id=>
<vorname>Rues
<nachname>Schu
<strasse>AmBa
source of the read function filehandle is opende elsewhere...

string xmlReadAttrib()
{
string tmpstr = "";
string data = "";
string type ="";
while(!infile.eof())
{
getline(infile, tmpstr);
type = tmpstr.substr((tmpstr.find("<",0)),(tmpstr.find("> ",0)));
cout << type << endl;
}
return type;
}
Dec 5 '06 #1
6 2039

Oliver Bleckmann napsal:
hi there,
i tried a lot, but it does not work correctly
it is more copied to the substr than expected,
should be only a "<tag>" but outputs "<tag>etc"

shuld parse a xml file like that:
<?xml version="1.0" encoding="iso8859-1"?>
<Hotel id="0" version="0">
<Room id=>
<number>0</number>
<Reservation id=>
<von>01/02/00</von>
<bis>05/41/12</bis>
<GuestA id=>
<vorname>Ruessel</vorname>
<nachname>Schuessel</nachname>
<strasse>AmBach</strasse>

but output is:
<?xml version="1.0" encoding="iso8859-1"?
<Hotel id="0" version="0"
<Room id=>
<number>0<
<Reservation id=>
<von>01/
<bis>05/
<GuestA id=>
<vorname>Rues
<nachname>Schu
<strasse>AmBa
source of the read function filehandle is opende elsewhere...

string xmlReadAttrib()
{
string tmpstr = "";
string data = "";
string type ="";
while(!infile.eof())
{
getline(infile, tmpstr);
type = tmpstr.substr((tmpstr.find("<",0)),(tmpstr.find("> ",0)));
cout << type << endl;
}
return type;
}
Method substr takes as 2nd parameter count of characters (NOT index of
last character).
You should also compare result of method find with std::string::npos
(which indicates 'not-found' case).

Dec 5 '06 #2
"Oliver Bleckmann" <Ol**************@freenet.dewrites:
hi there,
i tried a lot, but it does not work correctly
it is more copied to the substr than expected,
should be only a "<tag>" but outputs "<tag>etc"
Have a look at the Standard C++ Library Reference. You should find
something about string::members like this:

,----
| string substr(
| size_type _Off = 0,
| size_type _Count = npos
| ) const;
|
| Parameters
| _Off
| An index locating the element at the position from which the copy of
| the string is made, with a default value of 0.
|
| _Count
| The number of characters that are to be copied if they are present.
`----

[...]
type = tmpstr.substr((tmpstr.find("<",0)),(tmpstr.find("> ",0)));
cout << type << endl;
I guess, you want something like this:

size_t pa = tmpstr.find("<",0);
size_t len = tmpstr.find(">",0) - pa + 1;
type = tmpstr.substr(pa, len);
cout << type << endl;

Btw.: there are C++ libaries available to parse XML.
Btw2: you could perfectly use lex/yacc (flex/bison) to build your own
parser

Cheers,
Rudiger
Dec 5 '06 #3
On Dec 5, 11:22 pm, "Oliver Bleckmann" <Oliver-Bleckm...@freenet.de>
wrote:
hi there,
i tried a lot, but it does not work correctly
it is more copied to the substr than expected,
should be only a "<tag>" but outputs "<tag>etc"

shuld parse a xml file like that:
<?xml version="1.0" encoding="iso8859-1"?>
<Hotel id="0" version="0">
<Room id=>
<number>0</number>
<Reservation id=>
<von>01/02/00</von>
<bis>05/41/12</bis>
<GuestA id=>
<vorname>Ruessel</vorname>
<nachname>Schuessel</nachname>
<strasse>AmBach</strasse>

but output is:
<?xml version="1.0" encoding="iso8859-1"?
<Hotel id="0" version="0"
<Room id=>
<number>0<
<Reservation id=>
<von>01/
<bis>05/
<GuestA id=>
<vorname>Rues
<nachname>Schu
<strasse>AmBa

source of the read function filehandle is opende elsewhere...

string xmlReadAttrib()
{
string tmpstr = "";
string data = "";
string type ="";
while(!infile.eof())
{
getline(infile, tmpstr);
type = tmpstr.substr((tmpstr.find("<",0)),(tmpstr.find("> ",0)));
cout << type << endl;
}
return type;

}
Just to point out something with your code.
while(!infile.eof()) is not really a very safe way to stop on errors
with the stream.

A better alternative is
while(infile.good())

Since a stream can be set to an error state by something other than an
end-of-file.

Dec 5 '06 #4

"scorp007" <sc******@gmail.comschrieb im Newsbeitrag
news:11**********************@j44g2000cwa.googlegr oups.com...
A better alternative is
while(infile.good())

Since a stream can be set to an error state by something other than an
end-of-file.
changes made.
Dec 5 '06 #5

"Ondra Holub" <on*********@post.czschrieb im Newsbeitrag
news:11*********************@n67g2000cwd.googlegro ups.com...
>
Method substr takes as 2nd parameter count of characters (NOT index of
last character).
You should also compare result of method find with std::string::npos
(which indicates 'not-found' case).
Damn right, sir!
My fault.
Dec 5 '06 #6
JE

Oliver Bleckmann wrote:
"scorp007" <sc******@gmail.comschrieb im Newsbeitrag
news:11**********************@j44g2000cwa.googlegr oups.com...
A better alternative is
while(infile.good())

Since a stream can be set to an error state by something other than an
end-of-file.

changes made.
Bad idea. Try while(getline(infile, tmpstr)). See FAQ.

Dec 5 '06 #7

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

Similar topics

16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
2
by: Chris Bolus | last post by:
I'm a teacher using MS Access on an RMConnect 2.4 network. On some workstations both I and my students sometimes get an error message when attempting to insert a command button on a form which...
5
by: rachelm81 | last post by:
Hello, all! I'm creating a macro for help with some c/c++ projects, and within it, I'm trying to execute Find/Replace. Actually, it's executing fine. It's closing the prompt that I'm having a problem...
2
by: Mike | last post by:
Hello I load a text file into a string. Then I start seraching for a substring that appears many time in the file. Once I find an occurrence of the substirng I have to find the first accurance of...
9
by: Miky | last post by:
I have the following problem: I am populating a dataset but, sometimes (NOT always), the NewRow method works like the Add method. SetRightDetail.RightDetail.Rows.Clear() For Each Right In...
8
by: Chris Noble | last post by:
I need to check whether a particular user is already a member of an Active Directory Security Group. The following code extract works but only if the user distinguished name is exactly the same...
3
by: Rene | last post by:
Hello to all! For a long time I have been "fighting" a problem compiling an OpenGL program which uses GLUT. First I have put a question in a Watcom group (I want to use this compiler) to which I...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
6
by: jephperro | last post by:
Hi there, I'm having a really tough time with a SQL statement and I am wondering if someone is able to help out or point me in the right direction. I have a table of names which can be very...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...
0
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,...

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.