C++ Ostream Dev Null
I'm trying to createa function that takes command line arguments, parses them, and returns a stream to be used for output. If a filename is specified, the program will write to that file. If no filename is specified, the default output will be to cout.
I haven't gotten to the parsing yet. Simply passing an ostream parameter and trying to assign it to equal cout is giving me preoblems already. The error is one of those really long errors, so I won't post it unless someone wants it. I've tried making line 15 either an ostream or an ofstream. Neither works.
- Nov 27, 2017 Questions: I’m looking for a std::ostream implementation that acts like /dev/null. It would just ignore anything that is streamed to it. Does such a thing exist in the standard libraries or Boost? Or do I have to roll my own? Answers: If you have boost, then there’s a null ostream & istream implementation available in.
- This code uses a filebuf object (derived from streambuf) to open the file test.txt.The buffer is then passed as parameter to the ostream constructor, associating it to the stream. Objects of class ostream are seldom constructed directly. Generally some derived class is used (like the standard ofstream and ostringstream).
Concepts library (C20) Diagnostics library: Utilities library: Strings library: Containers library: Iterators library: Ranges library (C20) Algorithms library: Numerics library: Input/output library: Localizations library: Regular expressions library (C11) Atomic operations library (C11) Thread support library (C11) Filesystem library.
- 3 Contributors
- forum 5 Replies
- 3,393 Views
- 6 Hours Discussion Span
- commentLatest Postby VernonDozierLatest Post
Recommended Answers
The easiest thing in this case is to just use a smart pointer, as so:
Jump to PostAll 5 Replies
mike_2000_172,669
You cannot do what you are trying to do because objects in C++ are not references, they are objects. When you create the outs
object in main(), you create an object, not a reference to another object, so you cannot 're-seat' it by making it refer to something else (in particular, std::cout
). One simple way to achieve what you are trying to achieve is to do the following:
That's one simple way to do things, it is not super-robust, of course, you should implement the global-out as a proper singleton implementation, but you get the idea. And, technically, outputs that would occur after main has finished would be unsafe, but that's always the case anyways.
- C++ Basics
C++ Std Ostream
- C++ Object Oriented
- C++ Advanced
- C++ Useful Resources
- Selected Reading
It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries, including iostream. Consider the following program −
When the above code is compiled and executed, it produces the following result −
On most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.
To check for a null pointer you can use an if statement as follows −
C++ Ostream Reference
Thus, if all unused pointers are given the null value and you avoid the use of a null pointer, you can avoid the accidental misuse of an uninitialized pointer. Many times, uninitialized variables hold some junk values and it becomes difficult to debug the program.