从.txt文件解析html

时间:2015-09-13 13:58:08

标签: c++ qt parsing download

所以我得到了这个代码,我下载了一个html,将其保存在一个新创建的.txt文件中,然后解析该文件。 我有一个错误。每当我更改.txt文件的路径下载/创建它时,我第一次启动应用程序时,我的解析函数出现文件打开错误。 但是我第二次运行它一切都很好。

我一直在做一些阅读,它说replyFinished()是一个代码在下载完成后执行的地方,所以我认为这是我应该调用Do_parse()的地方,但它不起作用。

(将排除包含以保持更短)

·H

#ifndef DOWNLOADER_H
#define DOWNLOADER_H

class downloader : public QObject
{
    Q_OBJECT
public:
    explicit downloader(QObject *parent = 0);

    void Do_download();

    std::string getExePath();

    void Do_parse(QNetworkReply *reply);

public slots:
    void replyFinished (QNetworkReply *reply);

private:
    QNetworkAccessManager *manager;
    bool downloadSuccess;

};

#endif // DOWNLOADER_H

的.cpp

downloader::downloader(QObject *parent) :
    QObject(parent)
{
    downloadSuccess = false;
}


std::string downloader::getExePath()
{
    char result[MAX_PATH];
    return std::string(result,GetModuleFileNameA(NULL, result, MAX_PATH));
}

void downloader::Do_download()
{
    manager = new QNetworkAccessManager(this);

    connect(manager, SIGNAL(finished(QNetworkReply*)),
            this,SLOT(replyFinished(QNetworkReply*)));

            manager->get(QNetworkRequest(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
}

void downloader::replyFinished(QNetworkReply *reply)
{

    if(reply->error())
    {
        QMessageBox msgBox;
        msgBox.setText(strings::msgBoxDownReplyError);
        msgBox.exec();
        downloadSuccess = false;
    }
    else
    {

        downloadSuccess = true;

    }

    if(downloadSuccess)
        Do_parse(reply);
}

void downloader::Do_parse(QNetworkReply *reply)
{

        QFile *file = new QFile(strings::filePathQt);
        QByteArray data = reply->readAll();

        if(file->open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
            {
            file->write(data);
            file->flush();
            file->close();
        }
        delete file;

    reply->deleteLater();

}

·H

#ifndef PARSER_H
#define PARSER_H
    class parser 

{

public:
     explicit parser();

    QStringList currency_list;
    std::list<std::string> curr_list;

    void process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string);
    int read_line(std::map<std::string,double> &my_map);

private:

};

#endif // PARSER_H

的.cpp

parser::parser()   
{
        init_list();

}
void parser::process_line(std::string line, std::map<std::string,double> &my_map, std::string curr_string)
{
    double curr;
    std::string temp;

    temp = line.substr(52,8);

    std::replace(temp.begin(),temp.end(),',','.');

    curr = std::stod(temp,NULL);

    my_map.insert(std::make_pair(curr_string,curr));

}


int parser::read_line(std::map<std::string,double> &my_map)
{

    std::list<std::string>::iterator iter;
    std::string linija;
    std::ifstream file_(strings::filePathStd);

    if(file_.fail())
    {
        QMessageBox msgBox;
        msgBox.setText(strings::msgBoxOpeningError); // this msg error pops
        msgBox.exec();
    }

    else if(file_.is_open())
    {
        while(std::getline(file_,linija))
        {
            for(iter=curr_list.begin(); iter != curr_list.end(); ++iter)
            {
            if(linija.find(*iter) != std::string::npos)
            process_line(linija, my_map, *iter);
            }
        }
        file_.close();
    }

    return 0;
}

和我的主要

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    std::map<std::string,double> currency_map;

    downloader d;
    d.Do_download();
    parser p;
    p.read_line(currency_map);

return a.exec();
    };

0 个答案:

没有答案