c++ error saving to file using stream

1.7k Views Asked by At

I'm doing some code where i redirect the output to a file, but i get error using the fstream, ostream, streambuf(). It says that

-"fstream" is ambiguous

-"ostream" is ambiguous

-class "std::basic_ostream>" has no member "streambuf"

-class "std::shared_ptr" has no member "close"

I already search it for includes, installed the lastest version of the casablanca's rest api, ... and i still get those erros.. its missing some include?

Here is the code

#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

There are 2 best solutions below

0
On

-"fstream" is ambiguous

-"ostream" is ambiguous

-class "std::basic_ostream>" has no member "streambuf"

You are clashing std:: and casablanca's concurrency::streams namespaces, either make sure they are never pulled in with using in one file or use concurrency::streams explicitly

-class "std::shared_ptr" has no member "close"

but it does not! use -> on your fileStream


EDIT: I think your code is nothing but slightly modified version of official sample, you can just double check that you got it right

1
On

I had a similar problem, and indeed it's all to do with the namespace declaration & usage.

The comments above told only half the story.

This is what I ended up with for that application & it works:- (using VS2019)

//using namespace std;

using namespace concurrency::streams;

stringstreambuf buffer;

auto fileStream = std::make_shared();

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

{ *fileStream = outFile; etc . . .