fork(2) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5. //#include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. struct tagattribute {
  10. string name;
  11. string value;
  12. };
  13.  
  14. struct tagline {
  15. string tag;
  16. vector<tagattribute> attributes;
  17. };
  18.  
  19. int main() {
  20. vector<tagline> information;
  21. string line;
  22.  
  23. //ifstream readFile("file.txt");
  24. istream &readFile = cin;
  25.  
  26. while (getline(readFile, line)) {
  27. istringstream in(line);
  28.  
  29. tagline t;
  30. tagattribute attr;
  31.  
  32. in >> ws;
  33.  
  34. char ch = in.get();
  35. if (ch != '<')
  36. continue;
  37.  
  38. if (!(in >> t.tag))
  39. continue;
  40.  
  41. do
  42. {
  43. in >> ws;
  44.  
  45. ch = in.peek();
  46. if (ch == '>')
  47. {
  48. in.ignore();
  49. break;
  50. }
  51.  
  52. if (getline(in, attr.name, '=') &&
  53. in.ignore() &&
  54. getline(in, attr.value, '"'))
  55. {
  56. t.attributes.push_back(attr);
  57. }
  58. else
  59. break;
  60. }
  61. while (true);
  62.  
  63. information.push_back(t);
  64. }
  65.  
  66. vector<tagline>::iterator it = information.begin();
  67. for(; it != information.end(); ++it) {
  68. cout << "Tag: " << it->tag << "\n";
  69.  
  70. vector<tagattribute>::iterator it2 = it->attributes.begin();
  71. for(; it2 != it->attributes.end(); ++it2) {
  72. cout << "name: " << it2->name << "\n"
  73. << "value: " << it2->value << "\n";
  74. }
  75.  
  76. cout << "\n";
  77. }
  78.  
  79. return 0;
  80. }
Success #stdin #stdout 0s 4356KB
stdin
<tag1 attr1="value1" attr2="value2" >
<tag2 attr1="value1" attr2="value2" >
stdout
Tag: tag1
name: attr1
value: value1
name: attr2
value: value2

Tag: tag2
name: attr1
value: value1
name: attr2
value: value2