You could just go through the file and when your program comes across a '<' it should just ignore everything after it until it comes across a '>', at that point you've to count a tag ...
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
probably something like this:
std::string str = "<html>";
if( str.find("<") != string::npos && str.find(">") != string::npos)
{
// most likely an html tag
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Show us the code, with CODE TAGS.
MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
why not just use getline() to read an entire line at one time?
std::string line;
while( getline(ipfile, line) )
{
// blabla
}
I don't do html coding, but I think any given tag must be on one line, such as "" can not be split between lines, so it doesn't make any sense to read the html file one character at a time.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
you use getline() to read an entire line that is terminated with '\n'. Then use string::find() to look for < and > characters as shown in previous example code.
Also note that ipfile.eof() is not needed in my loop because the loop stops on error or end-of-file.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
You could tokenize (strtok) by "<" and stick each piece into a vector or so... then get it's size().
Comatose
Taboo Programmer
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
You could tokenize (strtok) by "<" and stick each piece into a vector or so... then get it's size().
That sounds like the hard way. strtok() embeds NULLs in the string, which might corrupt the std::string class.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>You could tokenize (strtok) by "<" and stick each piece into a vector or so... then get it's size().
Best not to mix c with c++. Naughty naughty :-0
In any case all these suggestions are too niave. There are a few examples which will slip through the loop, (html attributes)
http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx
So using regular expressions, would be the best way to deal with this however, if this is homework your professor is unlikely to care.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I think I am doing the same assignment as you. Dont forget there maybe an exclamation mark in normal text. "Hello!" you would be better saying if '<' precedes '!' then add 1 to comment count.
Xlphos
Veteran Poster
1,108 posts since Apr 2008
Reputation Points: 32
Solved Threads: 118