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 

#include 
#include 
#include 
//
#include 
#include 
#include 
#include 
//
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="<

            while(!in.atEnd())
            {
                QString line=in.readLine();
                QStringList fds=line.split("	");

                //std::cout<
                //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

你可能感兴趣的:(Qt,qt,开发语言)