簡體   English   中英

std :: ofstream在DLL中的行為不同

[英]std::ofstream behaves differently in DLL

我正在開發一個插件。 看看下面的代碼。

string request(char post_params[]) {
    CURL *curl;
    CURLcode res;
    std::string buffer; //here we'll write response

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_params);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(post_params));
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);

        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }

    return buffer;
}
....
bool perform(..) {
    std::ofstream file ("d:/t/t.txt");
    file << "opened";
    file.close();       
    string resp = request(....); 
    ...
}

如果代碼在應用程序內啟動,則會創建文件d:/t/t.txt ,但如果代碼編譯為DLL,並從運行我的插件的應用程序啟動,則不會創建該文件。 但是如果我注釋掉string resp = request(....); 接下來, 創建該文件。 有人可以解釋一下這里有什么?

如果使用Visual Studio,請確保將msvcprtd.lib(Debug)和msvcprt.lib(Release)添加到依賴項中。

std::ofstream file ("d:/t/t.txt");

// Make sure the file is opened before trying to write in it
if (!file.is_open())
{
   // print error message
}
else
{
   file << "opened";
   file.close();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM