I cant understand how date_and_time
is a char
when it has multiple characters
It is not a single char
, it is a char*
pointer to a null-terminated array of char
s.
when I want to get it to a string, it just stops when spaces come.
Because that is how operator>>
works. It stops reading on whitespace or EOF, whichever occurs first.
I tried putting it with or without noskipws but to no avail.
noskipws
before the first >>
tells it not to skip leading whitespace. But your input string does not have any, so the noskipws
is effectively a no-op in this situation. And it has no effect on the 2nd >>
because the 1st one clears that flag from the stream after it is done reading.
If printing date_and_time
is something like "Thu Jul 01 23:52:46 2021"
, when it turns to string it is just "Thu"
.
Because you are using >>
to parse the string. This is normal behavior.
If you want the whole string, use std::getline()
instead:
stringstream ss;
ss << date_and_time;
/* alternatively:
istringstream ss(date_and_time);
*/
getline(ss, date_and_time_string);
Or, since there is really no reason to use std::(i)stringstream
at all in this situation, you can just assign date_and_time
as-is directly to date_and_time_string
:
date_and_time_string = date_and_time;