I'd like to send data to nowhere, I mean that I don't want to print data in console nor in file, but I need some std::ostream
object. How to do that?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Print background image on every page once
- Converting glm::lookat matrix to quaternion and ba
The simplest solution is just to output to an unopened
std::ofstream
(or any other output stream in an error state). This will result in the stream being permanently in an error state. This could be an advantage (<<
operators will skip the formatting), but if any code that you can't control checks for errors, and does something particular if they occur, you'll likely have problems.Otherwise, it's pretty simple to implement a null stream; the only
streambuf
function you really have to override isoverflow
. Something like the following should do the trick:(The buffer will avoid some unnecessary virtual function calls. On some platforms, this makes a significant difference.)
Then create an output stream which uses it:
(The use of inheritance, rather than containment, ensures that the streambuf is fully constructed before being passed to the
ostream
. This generally isn't necessary in practice, but the standard doesn't seem to authorize passing a not yet constructedstreambuf
to the constructor ofostream
.)Since nobody mentioned it, if it's about suppressing std or error output, you can simply close the corresponding file descriptors (e.g.
fclose (stdout)
orfclose (stderr)
).That will shup up everything, including things like
printf
orfprintf (stderr, ...)
So you will indeed keep using the usual
cout
orcerr
, but they will be turned into bit buckets.I've used:
recently without problems, although it was flagged as having some potential problems if you looked at it from a certain angle (see the link below).
The following program shows it in action:
The page where I got it from also had this as a probably-cleaner solution (slightly modified to remove the duplication of my first solution above):
Simplest solution: Use a
std::stringstream
.The
stringstream
will contain the output, but if you don't use it, it's the same as if it was never filled.Some suggestions here http://bytes.com/topic/c/answers/589209-std-null-stream
A good answer from that site
Use ordinary std::fstream, open it only for writing to required file "/dev/null". It should work.
If you really want to create own stream, just derive it from basic_ostream and simply define your own operator<< to be function which only returns stream reference. You will have to write dummy 'write' and 'put' method too (and all other methods for output).
In fact,
From http://bytes.com/topic/c/answers/428285-null-ostream