c++ count html tags

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 8
Reputation: mat1989 is an unknown quantity at this point 
Solved Threads: 0
mat1989 mat1989 is offline Offline
Newbie Poster

c++ count html tags

 
0
  #1
Apr 5th, 2009
Hi guys.

Im doing an assignment, I am getting on ok with it but I am sadly really stuck at the moment.

The part I am stuck on involves counting html tags in a text file. I have thought of a method of doing this but unfortunately I have no idea how to implement it into the code. My idea is to look for the start symbol of the tag( <) then the contents, then the end symbol (>) Does anyone know how I could do this in c++?

Many thanks

Mat
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: c++ count html tags

 
0
  #2
Apr 5th, 2009
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 ...
Last edited by tux4life; Apr 5th, 2009 at 6:11 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: c++ count html tags

 
0
  #3
Apr 5th, 2009
probably something like this:
  1. std::string str = "<html>";
  2. if( str.find("<") != string::npos && str.find(">") != string::npos)
  3. {
  4. // most likely an html tag
  5. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 8
Reputation: mat1989 is an unknown quantity at this point 
Solved Threads: 0
mat1989 mat1989 is offline Offline
Newbie Poster

Re: c++ count html tags

 
0
  #4
Apr 5th, 2009
Thanks alot guys i'll try these ideas out.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 8
Reputation: mat1989 is an unknown quantity at this point 
Solved Threads: 0
mat1989 mat1989 is offline Offline
Newbie Poster

Re: c++ count html tags

 
0
  #5
Apr 5th, 2009
Dragon I had a stab at using your technique but it returns a random number of tags in the output.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 950
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: c++ count html tags

 
0
  #6
Apr 5th, 2009
Show us the code, with CODE TAGS.
Last edited by MosaicFuneral; Apr 5th, 2009 at 6:57 pm.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 8
Reputation: mat1989 is an unknown quantity at this point 
Solved Threads: 0
mat1989 mat1989 is offline Offline
Newbie Poster

Re: c++ count html tags

 
0
  #7
Apr 5th, 2009
  1. // assignment program
  2. // read file and copy to another
  3. // count amount of charecters,lines, comments and tags
  4. // change Xhtml tags from upper case to lower case
  5. // place in new file
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <string>
  10. #include <cstring>
  11. #include <iomanip>
  12. #include <cctype>
  13. using namespace std;
  14.  
  15. int main()
  16. {
  17. string file1,file2;
  18. string str = "<>";
  19. ifstream ipfile;
  20. ofstream opfile;
  21. char c;
  22.  
  23.  
  24.  
  25. int amountline = 0;
  26. int amountcha = 0;
  27. int amounttag = 0;
  28. int amountcomment = 0;
  29. cout << "Please enter the name of the file you wish to check" << endl;
  30. cin >> file1;
  31.  
  32.  
  33. ipfile.open(file1.c_str());
  34.  
  35.  
  36. if (!ipfile.is_open())
  37.  
  38. {
  39. cout << "Oops! Couldn't open " << file1 << "!\n"<<endl;
  40.  
  41. return 1;
  42.  
  43. }
  44.  
  45. {
  46.  
  47. cout << " Please enter the file you wish the edited contents to be copied to" << endl;
  48. cout << " This will be created if it does not already exist"<< endl;
  49. cin >> file2;
  50. }
  51.  
  52. opfile.open(file2.c_str());
  53.  
  54.  
  55. while (!ipfile.eof())
  56.  
  57. {
  58.  
  59. ipfile.get(c);
  60. opfile << c;
  61.  
  62.  
  63.  
  64.  
  65. if(c!='\n' && !ipfile.eof() && c!=' ')
  66. {
  67. amountcha++;
  68. }
  69. if(c=='\n')
  70. {
  71. amountline++;
  72. }
  73.  
  74. if( str.find("<") != string::npos && str.find(">") != string::npos)
  75. {
  76. amounttag++;
  77. }
  78.  
  79.  
  80. if ( c == '!')
  81. {
  82. amountcomment++;
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. cout << " This file contains :" << amountline << " lines" << endl;
  97. cout << " This file contains :" << amountcha << " charecters" << endl;
  98. cout << " this file has : " << amountcomment<< " comments " << endl;
  99. cout << " This file contains : " << amounttag << "tags" << endl;
  100.  
  101. cout << " Copy complete, edited code located in " << file2 << endl;
  102.  
  103.  
  104.  
  105. return 0;
  106. }

thats all my code so far.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: c++ count html tags

 
0
  #8
Apr 5th, 2009
why not just use getline() to read an entire line at one time?
  1. std::string line;
  2. while( getline(ipfile, line) )
  3. {
  4. // blabla
  5. }

I don't do html coding, but I think any given tag must be on one line, such as "<html>" can not be split between lines, so it doesn't make any sense to read the html file one character at a time.
Last edited by Ancient Dragon; Apr 5th, 2009 at 7:16 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 8
Reputation: mat1989 is an unknown quantity at this point 
Solved Threads: 0
mat1989 mat1989 is offline Offline
Newbie Poster

Re: c++ count html tags

 
0
  #9
Apr 5th, 2009
I can use getline to search for html tags in the line?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: c++ count html tags

 
0
  #10
Apr 5th, 2009
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.
Last edited by Ancient Dragon; Apr 5th, 2009 at 7:19 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC