problem with find on stirngs

Discussion in 'C++' started by Oliver Bleckmann, Dec 5, 2006.

  1. 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;
    }
     
    Oliver Bleckmann, Dec 5, 2006
    #1
    1. Advertising

  2. Oliver Bleckmann

    Ondra Holub Guest

    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).
     
    Ondra Holub, Dec 5, 2006
    #2
    1. Advertising

  3. "Oliver Bleckmann" <> writes:

    > 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
     
    Rud1ger Sch1erz, Dec 5, 2006
    #3
  4. Oliver Bleckmann

    scorp007 Guest

    On Dec 5, 11:22 pm, "Oliver Bleckmann" <>
    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.
     
    scorp007, Dec 5, 2006
    #4
  5. "scorp007" <> schrieb im Newsbeitrag
    news:...
    > 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.
     
    Oliver Bleckmann, Dec 5, 2006
    #5
  6. "Ondra Holub" <> schrieb im Newsbeitrag
    news:...
    >
    > 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.
     
    Oliver Bleckmann, Dec 5, 2006
    #6
  7. Oliver Bleckmann

    JE Guest

    Oliver Bleckmann wrote:
    > "scorp007" <> schrieb im Newsbeitrag
    > news:...
    > > 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.
     
    JE, Dec 5, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Jones

    Find::Find

    Dan Jones, Oct 7, 2004, in forum: Perl
    Replies:
    1
    Views:
    832
    Joe Smith
    Oct 8, 2004
  2. Wybo Dekker
    Replies:
    1
    Views:
    379
    Yukihiro Matsumoto
    Nov 15, 2005
  3. Stuart Clarke

    Slow Find.find - real problem

    Stuart Clarke, Sep 4, 2010, in forum: Ruby
    Replies:
    7
    Views:
    149
    Stuart Clarke
    Sep 6, 2010
  4. vdvorkin
    Replies:
    0
    Views:
    423
    vdvorkin
    Feb 10, 2011
  5. vdvorkin
    Replies:
    3
    Views:
    826
    vdvorkin
    Feb 14, 2011
Loading...

Share This Page