problem with find on stirngs

  • Thread starter Oliver Bleckmann
  • Start date
O

Oliver Bleckmann

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;
}
 
O

Ondra Holub

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).
 
R

Rud1ger Sch1erz

Oliver Bleckmann said:
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
 
S

scorp007

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.
 
O

Oliver Bleckmann

scorp007 said:
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.
 
O

Oliver Bleckmann

Ondra Holub said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top