I tried to write stream.rdbuf() to a file but it doesn’t seem to work, just a blank file, I don`t know what to do.
#include "stdafx.h"
#include <iostream>
#include <boost/asio.hpp>
#include <conio.h>
#include <stdio.h>
#include <fstream>
char buffer [9999999];
int main()
{
boost::asio::ip::tcp::iostream stream;
stream.expires_from_now(boost::posix_time::seconds(60));
stream.connect("www.mail.ru","http");
stream << "GET / HTTP/1.1\r\n";
stream << "Host mail.ru\r\n";
stream << "User-Agent Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11\r\n";
stream << "Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" ;
stream << "Accept-Encoding gzip,deflate,sdch\r\n";
stream << "Accept-Language en-US,en;q=0.8\r\n";
stream <<"Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n";
stream << "Cookie \r\n\r\n";
stream.flush();
using namespace std ;
cout << stream.rdbuf();
ofstream f("output.txt" /*| ios::bin*/);
f << stream.rdbuf();
f.close();
system("pause");
return 0 ;
}
You are invoking
stream.rdbuf()twice: once for the standard outcoutand then for the file.It is unclear from your explanation whether you see anything outputed on the screen (when you write to
cout).1) If yes, you need to write that to the file instead: by the time you read the stream again, it is exhausted and does not contain anything.
Just remove the
line.
If you need to see the result on the screen as well, capture it in a
std::stringstreamfirst, then you can output its content to both the screen and the file (useseekpon thestringstreamto reset it after the first extraction)2) If you do not see the results on the screen, you most likely have a problem with connecting to the service