Question

In: Computer Science

How to do in C++ HTML Converter Create a program that reads an HTML file and...

How to do in C++

HTML Converter

Create a program that reads an HTML file and converts it to plain text.

Console

HTML Converter

Grocery List

* Eggs

* Milk

* Butter

Specifications

  • Your instructor should provide an HTML file named groceries.html that contains these HTML tags:

<h1>Grocery List</h1>

<ul>

    <li>Eggs</li>

    <li>Milk</li>

    <li>Butter</li>

</ul>

  • When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the content and the HTML tags on the console as shown above.

Note

  • The groceries.html file ends each line with a carriage return character (‘\r’), not a new line character (‘\n’). To account for that, when you read the file, you can use the third parameter of the getline() function to specify the end of the line. For more information, you can search online and check the documentation of the getline() function.

Solutions

Expert Solution

#include<iostream>
#include<fstream>
#include<string>

using namespace std;
int main()
{
string line;
fstream myfile;
myfile.open("file.html", ios::in); // Read Html file
if (!myfile) // if file does not exits show error
cout << "file cannot open!";

bool inside = false; // initialize a boolian value inside as false
int i=0;
while (getline(myfile, line)) // Loop through each line of the file
{
   string result = ""; // define an emplty string result
for (int c=0;c<line.length();c++) // loop through each character of the line
       {
           // check for html tags
if (line[c] == '>')   
inside = true;
else if (line[c] == '<')
inside = false;
  
else if (inside && line[c]!='\t')
result +=line[c];
}
if(i<=0) //print the first line as it is.
cout<<result<<'\n';
else if(i>1) // print rest of the lines with an asterisks(*)
   cout<<"* "<<result<<'\n';
i++;
}

return 0;
}


Related Solutions

This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
Create a C Program that reads a file and replaces every other letter with an asterisk....
Create a C Program that reads a file and replaces every other letter with an asterisk. The first integer 'num' is the number of lines. Include an "Error" Message and exit the program if:    The wrong name for the input/output file is given    The input/output file can not be opened input.txt 7 Never Have We Ever Played A Game output.txt 7 N*v*r *a*e W* *v*r *l*y*d * G*m*
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Project 6-1: Email Creator C++ code Create a program that reads a file and creates a...
Project 6-1: Email Creator C++ code Create a program that reads a file and creates a series of emails. Console Email Creator ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi James, We've got some great deals for you. Check our website! ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi Josephine, We've got some great deals for you. Check our website! ================================================================ To:      [email protected] From:    [email protected] Subject: Deals! Hi Art, We've got some great deals for you. Check our website! Specifications...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT