I would like to parse date in format [mm/dd/yy]:
#include <sstream>
#include <iomanip>
#include <iostream>
#include <time.h>
int main()
{
struct tm time;
std::istringstream ss;
std::cout << "enter string [mm/dd/yy/]: ";
std::cin >> ss;
ss >> std::get_time(&time, "%D");
std::cout << time.tm_wday << std::endl;
}
But I cannot read from std::cin
to std::istringstream
, but why?
streambuf
. How is even the streambuf
implemented in C++? Is it an array? a struct?, Could I find the source implementation of it somewhere?You could use std::string
and std::getline
:
std::string input_text;
std::getline(std::cin, input_text);
std::istringstream input_text_stream(input_text);
No need of intermediate std::istringstream
, you might do directly:
std::cin >> std::get_time(&time, "%D");
To substitute underlying buffer, you have to call rdbuf
:
std::istringstream ss;
std::cout << "enter string [mm/dd/yy/]: ";
ss.basic_ios::rdbuf(std::cin.rdbuf()); // substitute internal ss buffer with std::cin one
ss >> std::get_time(&time, "%D");
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.