qt 5.15.2读取csv文件功能
工程文件.pro 内容:
QT = core#添加网络模块
QT += networkCONFIG += c++17 cmdline# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp
#include <QCoreApplication>#include <iostream>
#include <QFile>
#include <QTextStream>
//
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QUrl>
#include <QDir>
//
QNetworkAccessManager manager;
//
int printf(QString line)
{std::cout<<line.toStdString()<<std::endl;
}
int printf(int line)
{std::cout<<line<<std::endl;
}//异步下载
bool downloadFile(QString file_url,QString toDirPath)
{QDir dirPath(toDirPath);if(!dirPath.exists()){if(!dirPath.mkdir(dirPath.absolutePath())){printf("create dir error");return false;}}printf("old="+file_url);QUrl url(file_url); //"http://example.com/file.txt");QNetworkRequest request(url);// 发送GET请求获取文件数据QNetworkReply *reply = manager.get(request);// 处理请求完成信号QObject::connect(reply, &QNetworkReply::finished, [&]() {if (reply->error() == QNetworkReply::NoError) {// 获取响应数据QByteArray data = reply->readAll();QString toFilePath=toDirPath+"\\"+url.fileName();printf(toFilePath);// 保存到本地文件QFile file(toFilePath);if (file.open(QIODevice::WriteOnly)) {file.write(data);file.close();printf("文件下载成功:"+toFilePath);} else {printf("无法保存文件:"+toFilePath);}} else {printf("请求错误:");printf(reply->errorString());}// 清理资源reply->deleteLater();qApp->quit();});return true;
}//同步下载
bool syncDownloadFile(QString file_url,QString toDirPath)
{QUrl url(file_url);//输出C:\data\obj\test\Tile_+005_+006_OBJ.zipQString downloadFilePath=toDirPath+"\\"+url.fileName();printf(downloadFilePath);//QNetworkAccessManager manager;QNetworkReply *pReply = manager.get(QNetworkRequest(url));QEventLoop loop;manager.connect(pReply, SIGNAL(manager.finished()), &loop, SLOT(manager.quit()));loop.exec();if(pReply->error() != QNetworkReply::NoError){return false;}else{int code = pReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();if (code != 200){return false;}else{//下载成功,保存文件QFile localFile(downloadFilePath);if (localFile.open(QIODevice::WriteOnly)){localFile.write(pReply->readAll());localFile.close();}}}pReply->deleteLater();return true;
}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);std::cout<<"Hello World! hsg77"<<std::endl;//打开csv文件QFile file("E:\\QtProject\\objDownloadTest\\GridIdx_OBJ.csv");std::cout<<file.fileName().toStdString()<<std::endl;if(file.exists()){if(file.open(QIODevice::ReadOnly)){QTextStream in(&file);//std::cout<<"file state="<<in.atEnd()<<std::endl;while(!in.atEnd()){QString line=in.readLine();QStringList fds=line.split(" ");//std::cout<<line.toStdString()<<std::endl;//printf(fds.length());//处理每一行的数据QString fileUrl=fds.at(13);printf("readydown="+fileUrl);/*int i=0;for(const QString& fd : fds){printf(i);printf("="+fd);i+=1;}*/if(fileUrl!="FILE_URL"){//下载文件 fileUrlQString toDirPath="C:\\data\\obj\\test";syncDownloadFile(fileUrl,toDirPath);}//break;}}}//file.close();std::cout<<"csv file closed"<<std::endl;return a.exec();
}
本blog地址:
http://blog.csdn.net/hsg77