Please, I need help with this... The explanation is as important (if not more im
ID: 3553677 • Letter: P
Question
Please, I need help with this... The explanation is as important (if not more important) as the final solution. I just want to understand this :( Thank you in advance! (Using C++ and Visual Studio 2010. We can only use the basic libraries, no shortcuts to solve the problem).
The goal this week is to read in a basic HTML file and convert it into a plain text file. Your program should be able to read in an HTML file supporting the following elements:
<html>
<body>
<h1>Section Name</h1>
<p>This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.</p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
<p>This is a paragraph. This is a paragraph. This is a paragraph. </p>
<h1>Section Name 2</h1>
<p>This is a paragraph.</p>
</body>
</html>
As the HTML file is read in a new plain text file is created and the HTML is converted into plain text and written to this new file. The plain text for the above HTML example will look like:
Section Name
This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.
- List item 1
- List item 2
This is a paragraph. This is a paragraph. This is a paragraph.
Section Name 2
This is a paragraph.
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin;
ofstream fout;
fin.open("a.html");
fout.open("a.txt");
while(1) {
string s="";
getline(fin,s);
if(s=="")
break;
string out="";
bool tagOpen=false;
for(int i=0;i<s.length();i++) {
if(s[i]=='<' && tagOpen==false) {
tagOpen=true;
}
else if(s[i]=='>' && tagOpen==true) {
tagOpen=false;
}
else if(tagOpen==false){
out+=s[i];
}
}
if(out!="")
fout<<out<<endl;
}
return 0;
}