从没有标签的c ++读取html文件

时间:2018-06-14 21:18:22

标签: c++

这段代码应该用c ++读取一个html文件并显示没有标签的文件内容,但它不起作用。它没有显示任何东西。任何人都可以帮助我吗?

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
    int j = 0;
    string line;
    fstream myfile;
    myfile.open("ziba.txt", ios::in);
    if (!myfile)
        cout << "file cannot open!";
    while (getline(myfile, line))
    {
        cout << line << '\n';
    }
    cout << "\n" << endl;

    while (!myfile.eof())
    {
        if (line[j] == '>')
        {
            while (line[j] != '<')
            {

                myfile >> line[j];
                cout << line[j];
                j++;
            }
            j++;
        }
    }
    return 0;
}

2 个答案:

答案 0 :(得分:1)

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

using namespace std;

int main()
{
    string line;
    fstream myfile;
    myfile.open("ziba.txt", ios::in);
    if (!myfile)
        cout << "file cannot open!";

    bool inside = false;
    while (getline(myfile, line))
    {
        for (char c : line) {
            if (c == '>')
                inside = true;
            else if (c == '<')
                inside = false;
            else if (inside)
                cout << c;
        }
        cout << '\n';
    }

    return 0;
}

答案 1 :(得分:1)

您可以使用std::getline读取文件中的任何字符,这样它就不会只读取,它会一直读取您告诉它的任何字符。

std::istream& is = std::cin;  // or open a file
std::ostream& os = std::cout; // or open a file

std::string text;

// read up to the beginning of the next tag
while(std::getline(is, text, '<'))
{
    // print out what you have
    os << text;

    // read to the end of the tag
    std::getline(is, text, '>');

    // don't print this, it contains the tag itself
    // just loop back to reading up to the beginning
    // of the next tag.
}