繁体   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