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&gt;

#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:dataobjtestTile_+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

原文地址:https://blog.csdn.net/hsg77/article/details/134675335

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_4821.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注