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

qt 5.15.2读取csv文件功能

qt 5.15.2读取csv文件功能

工程文件.pro 内容:

QT = core

#添加网络模块
QT += network

CONFIG += 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.0

SOURCES += \
        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.zip
    QString 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")
                {
                    //下载文件  fileUrl
                    QString 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.kler.cn/news/149476.html

相关文章:

  • 一些数据库学习的小结
  • 【C++初阶】STL之学习string的用法
  • 【算法刷题】Day7
  • Python爬虫404错误:解决方案总结
  • nginx 配置跨域(小皮面板)
  • 鸿蒙4.0开发笔记之ArkTS语法的基础数据类型[DevEco Studio开发](七)
  • Mybatis代码生成器
  • 接口的跨域问题(CORS)
  • 接口测试工具(Jmeter)必学技巧
  • 在 go 的项目中使用验证器
  • 使用 DMA 在 FPGA 中的 HDL 和嵌入式 C 之间传输数据
  • 【算法刷题】Day8
  • 华为认证大数据工程师(HCIA-Big Data)--练习题
  • 在微服务架构中的数据一致性
  • 第二十章——多线程
  • 比尔盖茨:GPT-5不会比GPT-4好多少,生成式AI已达到极限
  • Jtti:linux中udp怎么判断是否接收到数据?
  • Linux 启动过程
  • hive- 18~18区间找最晚批次
  • 吃火锅(Python)
  • [个人笔记] Git的CLI笔录
  • cddd 安装指南(pip install cddd)
  • 延时任务定时发布,基于 Redis 与 DB 实现
  • 【蓝桥杯选拔赛真题26】C++字符串逆序 第十三届蓝桥杯青少年创意编程大赛C++编程选拔赛真题解析
  • Python之内置函数和模块
  • 小而美:持续盈利的经营法则
  • 医疗影像数据集—CT、X光、骨折、阿尔茨海默病MRI、肺部、肿瘤疾病等图像数据集
  • 汽车悬架底盘部件自动化生产线3d检测蓝光三维测量自动化设备-CASAIM-IS(2ND)
  • PC端ssh连接到Android手机的Termux部署http服务器
  • NX二次开发UF_MTX3_vec_multiply_t 函数介绍