使用流保存到文件的c ++错误

时间:2017-01-07 19:21:25

标签: c++ casablanca

我正在做一些代码,我将输出重定向到文件,但是我使用fstream,ostream,streambuf()得到错误。它说

- " fstream的"含糊不清

- " ostream的"含糊不清

-class" std :: basic_ostream>"没有会员" streambuf"

-class" std :: shared_ptr"没有会员"关闭"

我已经搜索了包含,安装了最新版本的卡萨布兰卡的休息api,...我仍然得到那些错误...它缺少一些包括?

这是代码

#include <sstream>
#include <iostream>
#include <fstream>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/http_listener.h>              // HTTP server
#include <cpprest/json.h>                       // JSON library
#include <cpprest/uri.h>                        // URI library
#include <cpprest/ws_client.h>                  // WebSocket client
#include <cpprest/containerstream.h>            // Async streams backed by STL containers
#include <cpprest/interopstream.h>              // Bridges for integrating Async streams with STL and WinRT streams
#include <cpprest/rawptrstream.h>               // Async streams backed by raw pointer to memory
#include <cpprest/producerconsumerstream.h>     // Async streams for producer consumer scenarios


using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams

using namespace web::http::experimental::listener;          // HTTP server
using namespace web::experimental::web_sockets::client;     // WebSockets client
using namespace web::json;



//Method

    auto fileStream = std::make_shared<std::ostream>();

    // Open stream to output file.
    pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)  //Error here on the fstrea and ostream
    {
        *fileStream = outFile;

        // Create http_client to send the request.
        http_client client(U("http://localhost:53213"));

        // Build request URI and start the request.
        uri_builder builder(U("/search"));
        builder.append_query(U("q"), U("cpprestsdk github"));
        return client.request(methods::GET, builder.to_string());
    })

        // Handle response headers arriving.
        .then([=](http_response response)
    {
        printf("Received response status code:%u\n", response.status_code());

        // Write response body into the file.
        return response.body().read_to_end(fileStream->streambuf());  //Error here on streambuf
    })

        // Close the file stream.
        .then([=](size_t)
    {
        return fileStream.close();  //Error on close

    });

    // Wait for all the outstanding I/O to complete and handle any exceptions
    try
    {
        requestTask.wait();
    }
    catch (const std::exception &e)
    {
        printf("Error exception:%s\n", e.what());
    }

2 个答案:

答案 0 :(得分:1)

  

- &#34; fstream的&#34;含糊不清

     

- &#34; ostream的&#34;含糊不清

     

-class&#34; std :: basic_ostream&gt;&#34;没有会员&#34; streambuf&#34;

您正在与std::和卡萨布兰卡的concurrency::streams命名空间发生冲突,要么确保它们永远不会被using放入一个文件中,也不会明确地使用concurrency::streams < / p>

  

-class&#34; std :: shared_ptr&#34;没有会员&#34;关闭&#34;

但它没有!在->

上使用fileStream

编辑:我认为你的代码只是官方sample的略微修改版本,你可以仔细检查你是否正确

答案 1 :(得分:0)

我有一个类似的问题,实际上,这全都与名称空间的声明和用法有关。

上面的评论只说明了一半。

这是我最终为该应用程序创建的内容,并且可以正常工作:- (使用VS2019)

//使用命名空间标准;

使用名称空间并发::流;

stringstreambuf缓冲区;

auto fileStream = std :: make_shared();

pplx :: task requestTask = fstream :: open_ostream(U(“ results.html”))。then([=](ostream outFile)

{ * fileStream = outFile; 等。 。 。