当前位置: 首页 > news >正文

qt 5.15.2读取csv文件功能

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

http://www.lryc.cn/news/247694.html

相关文章:

  • 【Vue】绝了!还有不懂生命周期的?
  • 关于IP与端口以及localhost
  • 如何进行MySQL的主从复制(MySQL5.7)
  • 5:kotlin 类(Classes )
  • 达梦:【1】达梦常用操作
  • 数字人透明屏幕的技术原理是什么?
  • 提升APP软件的用户体验方法
  • JVM运行时数据区域、对象内存分配、内存溢出异常总结
  • 【C++设计模式】单例模式singleton
  • CVPR 2023 精选论文学习笔记:Post-Training Quantization on Diffusion Models
  • Python基础语法之学习字符串快速格式化
  • Ubuntu22.04 server版本关闭DHCP,手动设置ip
  • 贪心算法(新坑)
  • C 语言头文件
  • MySQL中自增id用完怎么办?
  • C语言常见算法
  • 0-1背包的初始化问题
  • API之 要求接口上传pdf 以 合同PDF的二进制数据,multpart方式上传
  • C语言-求阶乘序列前N项和
  • 3:kotlin 逻辑控制(Control flow)
  • Linux系统之一次性计划任务at命令的基本使用
  • 记录:Unity脚本的编写8.0
  • OpenCV | 模版匹配
  • 【算法刷题】Day7
  • 前端 | iframe框架标签应用
  • linux -系统通用命令查询
  • python炒股自动化(1),量化交易接口区别
  • LeetCode(35)螺旋矩阵【矩阵】【中等】
  • BeanUtil.copyProperties的优化与使用(解决copyProperties null值覆盖问题)
  • Redis基本操作及使用