使用的QT图形界面,用libcurl获取的网页,在之中遇见了很多问题,一直想用c++类封装一下libcurl,发现c++很不到家啊。索性用了友元函数。
先贴上代码吧
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>#include <QtGui/QApplication>#define mydebug 0using namespace std;#include "Dict.h"FILE *fp; //定义FILE类型指针size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{int written = fwrite(ptr, size, nmemb, (FILE *)fp);return written;
}int getpage(QString aUrl)
{CURL *curl;QString mUrl="";mUrl.append("http://dict.baidu.com/s?wd=");mUrl.append(aUrl);mUrl.append("&tn=dict");curl_global_init(CURL_GLOBAL_ALL);curl=curl_easy_init();QByteArray ba = mUrl.toLatin1();const char *c_str2 = ba.data();curl_easy_setopt(curl,CURLOPT_URL,c_str2);if((fp=fopen("/home/archy/newfile","w"))==NULL){curl_easy_cleanup(curl);exit(1);}curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);curl_easy_perform(curl);curl_easy_cleanup(curl);return 0;
}int main(int argc, char *argv[])
{QApplication a(argc, argv);Dialog w;w.show();return a.exec();
}
Dict.h
#ifndef DICT_H
#define DICT_H#include <QtGui>
#include "translate.h"
#include "one.h"class QPushButton;
class QLineEdit;
class QTextEdit;class Dialog : public QDialog
{Q_OBJECTpublic:Dialog(QWidget *parent = 0);~Dialog();friend int getpage(QString aUrl);QString getResult();private slots:void clearItem();void searchItem();void UpdateClearBtn();void UpdateSearchBtn();private:QPushButton *mClearBtn;QPushButton *mSearchBtn;QLineEdit *mItem;QTextEdit *mResult;one *aone;
};#endif // DICT_H
Dict.cpp
1 #include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>
#include "Dict.h"
#include <QMessageBox>extern int getpage(QString aUrl);Dialog::Dialog(QWidget *parent): QDialog(parent)
{//init class of translate//aone = new one();//init controlsmClearBtn = new QPushButton("Clear");mSearchBtn = new QPushButton("Search");mClearBtn->setFlat(1);mClearBtn->setFixedSize(50,30);mSearchBtn->setFlat(1);mSearchBtn->setFixedSize(50,30);mItem = new QLineEdit;mResult = new QTextEdit;connect(mClearBtn,SIGNAL(clicked()),this,SLOT(clearItem()));connect(mSearchBtn,SIGNAL(clicked()),this,SLOT(searchItem()));QHBoxLayout *aHbox = new QHBoxLayout;aHbox->addWidget(mClearBtn);aHbox->addWidget(mItem);aHbox->addWidget(mSearchBtn);QVBoxLayout *aVbox = new QVBoxLayout;aVbox->addLayout(aHbox);aVbox->addWidget(mResult);setLayout(aVbox);
}QString Dialog::getResult()
{FILE *fp;char tem;char buffer[2000],final[200];int line = 1,counti=0,token = 0,finalcount=0;fp = fopen("/home/archy/newfile","r");while((tem = fgetc(fp))!=EOF){if(tem=='\n'){line++;if(line==427){break;}}//line++;}fgets(buffer,2000,fp);while(buffer[counti]!='\n' && counti < 2000){if(buffer[counti] == '<'){token = 0;}else if(buffer[counti] == '>'){token = 1;}else if(buffer[counti] != '>' && buffer[counti] != '<' && token == 1){final[finalcount] = buffer[counti];finalcount ++;}counti ++;}final[finalcount] = '\0';//QMessageBox msgBox;//msgBox.setText(final);//msgBox.exec();//QString string = QString(QLatin1String(final));
// QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));return string;
}void Dialog::clearItem()
{mItem->setText("");
}void Dialog::searchItem()
{if(mItem->text() != ""){getpage(mItem->text());QString astring = getResult();//QMessageBox msgBox;//msgBox.setText(astring);//msgBox.exec();mResult->setText(astring);}
}Dialog::~Dialog()
{}
还有一个问题就是乱码的问题。
因为是提取的网页,只能一个char一个char那样提取,现在还是没有想到什么其他办法提取。之后在char[] 转 QString 的时候,总是乱码。
哪位博友知道怎么解决的话,请给点线索。