Dec 5, 2011 at 8:26am UTC
A text file is hosted on the internet. www.a.com/b.txt
I want to read the file for input. How do I do this?
Dec 5, 2011 at 8:31pm UTC
Last edited on Dec 5, 2011 at 8:32pm UTC
Dec 5, 2011 at 8:41pm UTC
The most portable approach (which will likely become part of C++ in the next standard extension), is boost.asio
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
#include <boost/asio.hpp>
int main()
{
boost::asio::ip::tcp::iostream s("www.a.com" , "http" );
if (!s)
std::cout << "Could not connect to www.a.com\n" ;
s << "GET /b.txt HTTP/1.0\r\n"
<< "Host: www.a.com\r\n"
<< "Accept: */*\r\n"
<< "Connection: close\r\n\r\n" ;
for (std::string line; getline(s, line); )
std::cout << line << '\n' ;
}
As you can see, it is a bit more low-level than you probably need (you have to write HTTP protocol by hand). If that's not what you want, do try out third-party HTTP libraries:
curl++:
http://curlpp.org/
poco:
http://pocoproject.org/
cpp-netlib:
http://cpp-netlib.sourceforge.net/
Last edited on Dec 5, 2011 at 8:43pm UTC