C++ c++;使用流保存到文件时出错
我正在编写一些代码,将输出重定向到一个文件,但使用fstream、ostream、streambuf()时出错。上面说 -“fstream”是不明确的 -“奥斯特雷姆”是模棱两可的 -类“std::basic_ostream>”没有成员“streambuf” -类“std::shared_ptr”没有成员“close” 我已经在搜索包括,安装了卡萨布兰卡RESTAPI的最新版本。。。我还是会犯错误。。它少了一些吗 这是密码C++ c++;使用流保存到文件时出错,c++,casablanca,C++,Casablanca,我正在编写一些代码,将输出重定向到一个文件,但使用fstream、ostream、streambuf()时出错。上面说 -“fstream”是不明确的 -“奥斯特雷姆”是模棱两可的 -类“std::basic_ostream>”没有成员“streambuf” -类“std::shared_ptr”没有成员“close” 我已经在搜索包括,安装了卡萨布兰卡RESTAPI的最新版本。。。我还是会犯错误。。它少了一些吗 这是密码 #include <sstream> #include &l
#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());
}
#包括
#包括
#包括
#包括
#包括
#包含//HTTP服务器
#include//JSON库
#包含//URI库
#包括//WebSocket客户端
#包含//STL容器支持的异步流
#包括//用于将异步流与STL和WinRT流集成的桥
#包含//由指向内存的原始指针支持的异步流
#包括//生产者-消费者场景的异步流
使用命名空间实用程序;//常见的实用程序,如字符串转换
使用命名空间web;//常见的功能,如URI。
使用命名空间web::http;//通用HTTP功能
使用命名空间web::http::client;//HTTP客户端功能
使用命名空间并发::streams;//异步流
使用命名空间web::http::experimental::listener;//HTTP服务器
使用命名空间web::experimental::web_sockets::client;//WebSockets客户端
使用名称空间web::json;
//方法
auto fileStream=std::make_shared();
//打开流以输出文件。
pplx::task requestTask=fstream::open_ostream(U(“results.html”))。然后([=](ostream outFile)//fstrea和ostream上出现错误
{
*fileStream=outFile;
//创建http_客户端以发送请求。
http_客户端(U)http://localhost:53213"));
//生成请求URI并启动请求。
uri_生成器(U(“/search”);
附加查询(U(“q”)、U(“cpprestsdk github”);
返回client.request(方法::GET,builder.to_string());
})
//处理到达的响应头。
。然后([=](http_响应)
{
printf(“收到的响应状态代码:%u\n”,response.status_code());
//将响应主体写入文件。
返回response.body().read_to_end(fileStream->streambuf());//streambuf上出现错误
})
//关闭文件流。
。然后([=](大小)
{
返回fileStream.close();//关闭时出错
});
//等待所有未完成的I/O完成并处理任何异常
尝试
{
requestTask.wait();
}
捕获(const std::exception&e)
{
printf(“错误异常:%s\n”,例如what());
}
-“fstream”是不明确的
-“奥斯特雷姆”是模棱两可的
-类“std::basic_ostream>”没有成员“streambuf”
您正在冲突std::
和卡萨布兰卡的concurrency::streams
名称空间,请确保它们从未在一个文件中使用或显式使用concurrency::streams
-类“std::shared_ptr”没有成员“close”
但事实并非如此!在文件流上使用->
编辑:我认为您的代码只不过是对official稍加修改的版本,您只需仔细检查一下是否正确我也遇到了类似的问题,实际上这都与名称空间声明和用法有关
上面的评论只说明了事情的一半
这就是我在该应用程序中得到的结果&它是有效的:-
(使用VS2019)
//使用名称空间std
使用名称空间并发::streams
stringstreambuf缓冲区
auto fileStream=std::make_shared()
pplx::task requestTask=fstream::open_ostream(U(“results.html”))。然后([=](ostream outFile)
{
*fileStream=outFile;
等等…您是否尝试过使用std
前缀,例如std::fstream
?是的。如果我在此处添加std::fstream:fstream::open_ostream(U(“results.html”)。然后([=](ostream outFile),我会得到以下错误:类“std::basic_fstream”没有成员“open_ostream”@Tazz It没有open_ostream
成员。停止猜测,阅读一些文档。一本好书可能也是个好主意。我在rest api的官方文档中看到了这一点:您的fstream来自concurrency::streams,显式使用名称空间,以避免与std::不明确。请使用ctrl-k
正确地ormat代码摘录。