Custom Delimiters for C 's cin
Problem:
When using cin after redirecting it to a file stream (via cin.rdbuf(inF.rdbug())), it reads until reaching a white space character. Is it possible to use an alternative delimiter?
Answer:
Yes, it is possible to change the inter-word delimiter for cin (or any other std::istream) using std::ios_base::imbue and a custom ctype facet.
A custom facet that treats colons (:) and newlines as whitespace characters can be used to read files like /etc/passwd, where fields are separated by colons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
By applying this custom facet to the input stream, cin will now interpret colons and newlines as word delimiters, allowing for the separation of fields in files like /etc/passwd.
The above is the detailed content of Can C 's `cin` Use Custom Delimiters?. For more information, please follow other related articles on the PHP Chinese website!