I am starting to work with the Poco C++ libraries, especially for HTTP client/server parsing. I saw that there are classes such as HTTPRequest, HTTPResponse etc. and these have a method named "read(std::istream)". This method takes input argument of type "std::istream". However, I want to use this with something that i enter from the command line. I am using cin to take the input but this gives an error since istream and cin are of different types. Heres an example :
int main() {
HTTPRequest* req = new HTTPRequest();
std::string input;
std::cout << "Enter something.. " << std::endl;
std::cin >> input;
req->read(input);
}
My understanding is that the read method will interpret the data as HTTPRequest type. I am doing this just for testing. I know "string" type wont work, but i tried using istream constructor with getline etc and it still gives compile time error. So what is the ideal way to do this ?
According to the docs for POCO, the HTTPRequest ::read method takes an std::istream
object.
void read(
std::istream & istr
);
If you want to read the request from standard input, pass std::cin as a parameter.
int main() {
HTTPRequest* req = new HTTPRequest();
// std::string input;
// std::cout << "Enter something.. " << std::endl;
// std::cin >> input;
req->read(std::cin);
return 0;
}
When it tries to read from std::cin
, it will prompt you for input, so you can enter whatever you were trying to enter into the string you had there. I would then recommend, you either use an std::ifstream
object or use std::istringstream
. These both subclass std::istream
, so you can pass that as parameter.
Ex:
int main() {
HTTPRequest* req = new HTTPRequest();
std::string input;
std::cout << "Enter something.. " << std::endl;
std::cin >> input;
std::istringstream iss(input);
req->read(iss);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.