iostream

Hide user input on password prompt [duplicate]

老子叫甜甜 提交于 2019-12-17 06:41:28
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Read a password from std::cin I don't work normally with the console, so my question is maybe very easy to answer or impossible to do . Is it possible to "decouple" cin and cout , so that what I type into the console doesn't appear directly in it again? I need this for letting the user typing a password and neither me nor the user normally wants his password appearing in plaintext on the screen. I tried using

How can I compose output streams, so output goes multiple places at once?

浪子不回头ぞ 提交于 2019-12-17 04:02:27
问题 I'd like to compose two (or more) streams into one. My goal is that any output directed to cout , cerr , and clog also be outputted into a file, along with the original stream. (For when things are logged to the console, for example. After closing, I'd like to still be able to go back and view the output.) I was thinking of doing something like this: class stream_compose : public streambuf, private boost::noncopyable { public: // take two streams, save them in stream_holder, // this set their

std::cout won't print

夙愿已清 提交于 2019-12-17 03:43:18
问题 Is there any circumstance when std::cout << "hello" doesn't work? I have a c/c++ code, however the std::cout doesn't print anything, not even constant strings (such as "hello"). Is there any way to check if cout is able/unable to open the stream? There are some member functions like good() , bad() , ... but I don't know which one is suitable for me. 回答1: Make sure you flush the stream. This is required because the output streams are buffered and you have no guarantee over when the buffer will

Redirecting standard input of console application

。_饼干妹妹 提交于 2019-12-17 03:41:25
问题 I have a console application which I'm trying to automate by redirecting Standard input stream of the process. In manual mode after opening the application, it waits for user input like below, I created the process with redirected Standard input stream.The code snippet is as follows, Process newProcess = new Process(); newProcess.StartInfo.FileName = exeName; newProcess.StartInfo.Arguments = argsLine; newProcess.StartInfo.UseShellExecute = false; newProcess.StartInfo.RedirectStandardOutput =

Prevent scientific notation in ostream when using << with double

只愿长相守 提交于 2019-12-17 02:37:37
问题 I need to prevent my double to print in scientific notation in my file, when I do this outfile << X; 回答1: To set formatting of floating variables you can use a combination of setprecision(n), showpoint and fixed. In order to use parameterized stream manipulators like setprecision(n) you will have to include the iomanip library: #include <iomanip> setprecision(n) : will constrain the floating-output to n places, and once you set it, it is set until you explicitly unset it for the remainder of

How to read until EOF from cin in C++

拈花ヽ惹草 提交于 2019-12-17 02:34:15
问题 I am coding a program that reads data directly from user input and was wondering how could I (without loops) read all data until EOF from standard input. I was considering using cin.get( input, '\0' ) but '\0' is not really the EOF character, that just reads until EOF or '\0' , whichever comes first. Or is using loops the only way to do it? If so, what is the best way? 回答1: The only way you can read a variable amount of data from stdin is using loops. I've always found that the std::getline()

Which C I/O library should be used in C++ code? [closed]

可紊 提交于 2019-12-17 02:23:07
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . In new C++ code, I tend to use the C++ iostream library instead of the C stdio library. I've noticed some programmers seem to stick to

Read an unknown number of lines from console in c++

北战南征 提交于 2019-12-14 03:52:44
问题 I am using the following loop to read an unknown number of lines from the console, but it is not working. After I have fed the input I keeping pressing enter but the loop does not stop. vector<string> file; string line; while(getline(cin,line) file.push_back(line); 回答1: Because getline will evaluate to true even if you push only enter. You need to compare the read string to the empty string and break if true. vector<string> file; string line; while(getline(cin,line)) { if (line.empty()) break

How are iostream objects cin, cout, cerr, and clog implemented?

混江龙づ霸主 提交于 2019-12-14 03:42:45
问题 iostream objects cin, cout, cerr, and clog are objects declared in the iostream header. I'm aware that it's possible in some compilers to attempt to use these iostream objects before they are constructed, so under some circumstances they must be subject to the "static initialisation order fiasco". In those compilers where it's always safe to use std::cout et al, how do these objects actually get constructed? Does it involve under-the-hood compiler magic or could it in principle all be done

Taking ownership of streambuf/stringbuf data

半世苍凉 提交于 2019-12-13 20:06:20
问题 I'd like an interface for writing to an automatically resizing array. One way to do this is with a generic std::ostream * . Then consider if ostringstream is the target: void WritePNG(ostream *out, const uint8_t *pixels); void *WritePNGToMemory(uint8_t *pixels) { ostringstream out; WritePng(&out, pixels); uint8_t *copy = new uint8_t[out.tellp()]; memcpy(copy, out.str().c_str(), out.tellp()]; return copy; } But I want to avoid the memcpy(). Is there a way to take ownership of the array in the