After this assignment, you should be able to:

  • Write C++ functions and call them
  • Work with text files using the < fstream > library

Here are the requirements:

The program will read in the name of a text file, open it, and print to the screen any URL's found in HTML anchor elements

  • An anchor element looks like < a href="some URL" > text that is display for link < /a >.
  • If the file cannot be opened, display an error message to cerr, as shown below

The program needs to include at least two functions (you are free to write more):

  • find(ifstream &, string) - takes a reference to a file for input and reads words until either it finds the target string (the 2nd parameter) or the end of the file is reached.
    • Return true if the string is found, false if not
    • This function only detects the string if it is a separate work, not if it is a substring, so looking for "he" in "hello there" would return false
    • [Note that you have to pass the ifstream as a reference. Think about what it would mean to pass an ifstream by value.]
  • getURL(ifstream &) - this assumes you have opened and read the ifstream up to and including the < a. This function returns the string between the double-quotes after the href=
    • A useful function here is the version of getline() that accepts a 3rd parameter, a char that is the delimiter. The delimiter is what getline() uses to separate "lines". For getURL(), the double-quote character is exactly what we need as a delimiter

Your main() must call the above functions to do its job

  • Hint: the functions should not call each other, as their prototypes do not support helping each other

For a given file a.html like:

< !—This is a slightly edited
from https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a -- >
< p >You can reach Michael at:< /p >
< ul >
< li >< a href="https://example.com">Website< /a >< /li >
< li >< a href="mailto:m.bluth@example.com" >Email< /a >< /li >
< li >< a href=tel:+123456789 >Phone< /a >< /li >
< /ul >

Here is what the output should look like:

This program extracts and displays URLs from anchor elements in a HTML file

File to process: a.html
https://example.com
mailto:m.bluth@example.com
tel:+123456789

If the program cannot open the file, the output should look like:

This program extracts and displays URLs from anchor elements in a HTML file

File to process: b.html
Could not open b.html
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.