How to read from a c++ program till a character is read then skip some characters and continue reading again

ark picture ark · Apr 2, 2012 · Viewed 34.2k times · Source

How do i read from a file till a particular character is reached and then seek to the next character and continue reading in c++.

In my program i am using some HTML syntax and generating a htm file... So in my c++ code i have added the tags... but when i read from my htm file i want it to not include the tags.

What i plan on doing is reading the file till '<' is encountered then just seek to the point till '>' is encountered and continue reading from there..

Please help me and guide me with this.. I am not very experienced with file input output in c++.. Thank You..:)

Answer

CashCow picture CashCow · Apr 2, 2012

In general, to read a file until a particular character is reached you use std::getline and you set the second parameter to your terminator so if you are reading until a '<' character you can do

std::getline( infile, str, '<' );

you can then do the same with a > character

In your case if you are parsing HTML then there are probably specific parsers for it already. I think HTML1.1 is XML compliant but HTML1.0 isn't as it was not always necessary to close all your tags, so an XML parser will not necessarily work.

You would need to assume that open and close tags are not part of comments or quoted text and the methodology I described above would not promise you that so you'd need a full state machine.