以下是一个使用C++编写的程序,可以自动搜索指定目录下后缀为txt和zip的文件,并将它们上传到服务器。在运行程序之前,请确保已经安装了libcurl库。

#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include <curl/curl.h>

// 服务器上传URL
const std::string UPLOAD_URL = "http://example.com/upload.php";

// 上传文件到服务器
bool uploadFile(const std::string& filePath) {
    CURL* curl = curl_easy_init();
    if (curl) {
        // 设置上传URL
        curl_easy_setopt(curl, CURLOPT_URL, UPLOAD_URL.c_str());

        // 设置上传文件
        curl_easy_setopt(curl, CURLOPT_READDATA, fopen(filePath.c_str(), "rb"));

        // 执行上传
        CURLcode res = curl_easy_perform(curl);

        // 检查上传是否成功
        if (res != CURLE_OK) {
            std::cout << "上传文件失败: " << curl_easy_strerror(res) << std::endl;
            curl_easy_cleanup(curl);
            return false;
        }

        // 上传完成
        std::cout << "成功上传文件: " << filePath << std::endl;
        curl_easy_cleanup(curl);
        return true;
    }

    return false;
}

// 搜索目录下后缀为txt和zip的文件
void searchFiles(const std::string& directory) {
    std::vector<std::string> extensions = { ".txt", ".zip" };

    for (const auto& entry : std::filesystem::directory_iterator(directory)) {
        if (entry.is_regular_file()) {
            std::string filePath = entry.path().string();

            // 检查文件后缀
            for (const auto& extension : extensions) {
                if (filePath.substr(filePath.size() - extension.size()) == extension) {
                    // 上传文件
                    if (uploadFile(filePath)) {
                        // 如果上传成功,可以选择删除文件
                        // std::filesystem::remove(filePath);
                    }
                    break;
                }
            }
        }
    }
}

int main() {
    std::string directory = "path/to/directory";
    searchFiles(directory);
    return 0;
}

请根据你的实际需求修改以下内容:

  1. UPLOAD_URL的值替换为你的服务器上传URL。
  2. directory的值替换为你要搜索文件的目录路径。

运行该程序后,它会在指定目录下搜索后缀为txt和zip的文件,并将它们上传到服务器。你可以根据需要选择是否在上传成功后将文件删除

用c++帮我写个完整的文件运行自动寻找后缀带有txtzip的文件然后上传服务器。

原文地址: https://www.cveoy.top/t/topic/iWZK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录