0

I have this assignment to search for certian info in a html file and put the result into text file. I wanted to do it using getline, but somehow it's not working. I have no problems with using getline on text file so I assumed that you cannot use getline on html file. Is that assumption right? How can I convert such file into a text file? Or maybe there is a better/easier solution?Thanks.

Here is the code:(the names of the variables are not in english, I hope it's not a problem)

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

using namespace std;

string nazwa_wejsciowego;
string roboczy;
ifstream html;
ifstream txt("wynik.txt");
int main()
{
cout<<"Podaj nazwę pliku html, z ktorego odczytane maja zostac dane."<<endl;
cin>>nazwa_wejsciowego;

ifstream html(nazwa_wejsciowego, ios::app); //opening the file

if(!html){
    cout<<"Otwarcie pliku "<<nazwa_wejsciowego<<" nie powiodlo sie."<<endl;
    system("pause");}

 //checking if it opende properly

 getline(html, roboczy);
cout<<roboczy<<endl;

 return 0;}

1 Answers1

0

No, the assumption is not right. HTML is text; the fact that it is in a structured format that can be parsed by a computer to render a webpage is not relevant to read individual characters in the file.

getline may be a suitable approach though, as Steve points out in comments, some HTML pages are "minified" (they have unnecessary whitespace removed to save space and make code harder to copy) and, in such a case, you may end up with just one really big line. It may therefore be more convenient to read in chunks of bytes.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055