iostream

How should I manage ::std::cout after changing file descriptor 1 to refer to a different file?

随声附和 提交于 2021-02-19 01:22:37
问题 I would like to do dup2(fd, 1); close(fd); and have ::std::cout write to the new fd 1. How do I can reset the state of ::std::cout so nothing goes funny? For example, is flushing beforehand sufficient? Or is there more to do than that? I'm also curious about the same thing with ::std::cin . Is there a standard mechanism for resetting these if you change out the file descriptors they're using underneath them? To be clear, my goal here is basically to redirect my own input and output someplace

Why does std::basic_istream::ignore() extract more characters than specified?

天大地大妈咪最大 提交于 2021-02-18 10:55:46
问题 I have the following code: #include <iomanip> #include <iostream> #include <sstream> #include <string> using namespace std; int main(int argc, char* argv[]) { stringstream buffer("1234567890 "); cout << "pos-before: " << buffer.tellg() << endl; buffer.ignore(10, ' '); cout << "pos-after: " << buffer.tellg() << endl; cout << "eof: " << buffer.eof() << endl; } And it produces this output: pos-before: 0 pos-after: 11 eof: 0 I would expect pos-after to be 10 and not 11 . According to the

What's the difference between std::endl and '\n'

蓝咒 提交于 2021-02-09 11:10:46
问题 I've read the difference between std::endl and '\n' is that std::endl flushes the buffer and '\n' doesn't. However, as far as I know stdout on linux is line-buffered anyway, so does it mean that std::cout << ... << std::endl is the same as std::cout << ... << '\n' ? 回答1: std::ostream os; os << std::endl; // more concise os << '\n' << std::flush; // more explicit about flushing Those two lines have the exact same effect. The manual flushing is often a waste of time: If the output stream is

how to include C++ input stream delimiters into result tokens

橙三吉。 提交于 2021-02-09 07:10:27
问题 C++ standard library supports a few ways to introduce custom delimiters for input streams, as I understand recommended way is a using new locale and ctype objects: first way (inherited from ctype specialization) : struct csv_whitespace : std::ctype<char> { bool do_is(mask m, char_type c) const { if ((m & space) && c == ' ') { return false; // space will NOT be classified as whitespace } if ((m & space) && c == ',') { return true; // comma will be classified as whitespace } return ctype::do_is

how to include C++ input stream delimiters into result tokens

自闭症网瘾萝莉.ら 提交于 2021-02-09 07:00:41
问题 C++ standard library supports a few ways to introduce custom delimiters for input streams, as I understand recommended way is a using new locale and ctype objects: first way (inherited from ctype specialization) : struct csv_whitespace : std::ctype<char> { bool do_is(mask m, char_type c) const { if ((m & space) && c == ' ') { return false; // space will NOT be classified as whitespace } if ((m & space) && c == ',') { return true; // comma will be classified as whitespace } return ctype::do_is

Formatting the output stream, ios::left and ios::right

放肆的年华 提交于 2021-02-08 13:14:23
问题 I have this code: cout << std::setiosflags(std::ios::right); cout << setw(3) << 1 << setw(3) << 2 << '\n'; // Output two values cout << std::setiosflags(std::ios::left); cout << setw(3) << 1 << setw(3) << 2 << '\n'; // Output two values but the output doesnt come like i expected. instead of: 1 2 1 2 this comes out: 1 2 1 2 What is the problem? I set 'std::ios::left' but it makes no difference? 回答1: You have to clear the previous value in adjustfield before you can set a new one. Try this:

Using cin >> to error handle

梦想与她 提交于 2021-02-08 09:16:40
问题 I came across this in my readings... while(!(std::cin >> array[i])) { std::cin.clear(); while(std::cin.get()!= '\n') continue; std::cout << "enter a new input: "; } And, I don't really understand how the error handling is working. std::cin.clear() is used but the code continues to get characters from the cin object in the next line and then uses a continue statement. What exactly does the clear do if it doesn't clear the cin? Thank you. 回答1: what cin.clear() does is to clear the cin stream's

Using cin >> to error handle

限于喜欢 提交于 2021-02-08 09:16:34
问题 I came across this in my readings... while(!(std::cin >> array[i])) { std::cin.clear(); while(std::cin.get()!= '\n') continue; std::cout << "enter a new input: "; } And, I don't really understand how the error handling is working. std::cin.clear() is used but the code continues to get characters from the cin object in the next line and then uses a continue statement. What exactly does the clear do if it doesn't clear the cin? Thank you. 回答1: what cin.clear() does is to clear the cin stream's

Why use showpoint when you can use setprecision fixed?

杀马特。学长 韩版系。学妹 提交于 2021-02-07 08:36:54
问题 I don't quite understand the purpose of showpoint, i know it forces to show a decimal point, but having "cout << setprecision << fixed" is enough without the use of showpoint. Can you show me an example where showpoint is a must have? 回答1: When covering a large range of values it may be desirable to have the formatting logic to switch between the use of fixed point and scientific notation while still requiring a decimal point. If you look at the output of this example you'll see that it isn't

Why use showpoint when you can use setprecision fixed?

谁说我不能喝 提交于 2021-02-07 08:36:00
问题 I don't quite understand the purpose of showpoint, i know it forces to show a decimal point, but having "cout << setprecision << fixed" is enough without the use of showpoint. Can you show me an example where showpoint is a must have? 回答1: When covering a large range of values it may be desirable to have the formatting logic to switch between the use of fixed point and scientific notation while still requiring a decimal point. If you look at the output of this example you'll see that it isn't