ostream

How to close ofstream after assigning to ostream?

对着背影说爱祢 提交于 2019-12-10 18:07:40
问题 I can do std::ostream& out = condition ? std::cout : std::ofstream(filename); but how do I close in case of out = std::ofstream(filename) ? 回答1: As I understood you want to close file stream using out ? You don't need to close it explicitly. std::fstream is RAII object, so it will close an opened file automatically at the end of enclosing scope. And of course, you can always cast out if you really need to close the file just now: if( ptr = dynamic_cast<std::ofstream*>(out) ) { ptr->close(); }

Overload << operator to change “ ” to “\n”

谁说胖子不能爱 提交于 2019-12-10 17:08:37
问题 I am trying to overload << operator. For instance cout << a << " " << b << " "; // I am not allowed to change this line is given I have to print it in format <literal_valueof_a><"\n> <literal_valueof_b><"\n"> <"\n"> I tried to overload << operator giving string as argument but it is not working. So I guess literal " " is not a string. If it is not then what is it. And how to overload it? Kindly help; Full code //Begin Program // Begin -> Non - Editable #include <iostream> #include <string>

Visual C++ 2012 cout crashes during run time

南楼画角 提交于 2019-12-10 04:24:46
问题 I decided to try Visual Studio 2012 Express today. First thing to do was to write "Hello world!" application, however, I couldn't make it work. I created a Windows console application project, wrote standard code and it resulted in a run-time error. Here's my code: #include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; system("pause"); return 0; } Looks like something is broken (maybe I missed something?). It gets a runtime error at line 7: http://img443

How to have all platform compiler output the same string for NaN?

梦想的初衷 提交于 2019-12-10 03:07:23
问题 Consider this code snippet: #include <iostream> #include <string> #include <limits> int main() { std::cout << std::numeric_limits<double>::quiet_NaN(); } When compiled with Visual Studio 2010, output is 1.#QNAN . When compiled with g++, output is nan . Note that Visual Studio 2015 outputs "nan". However, I need both to produce the same output. What's the most simple way to do that? I tried to override operator<< for double but I feel like that's not the right way to do. Can string to be used

Is there a good idiom to deal with alternative output streams?

会有一股神秘感。 提交于 2019-12-09 20:24:50
问题 I want to write a simple program that depending on the options passed it the executable will print the output to the screen or to a file. The program is simple. #include<iostream> int main(int argc, char* argv[]){ ... process options... std::ostream& out = ... // maybe std::cout, maybe a *new* std::ofstream; out << "content\n"; } Is there a good idiom to make out refer alternatively to std::cout or a file stream at runtime? I tried with pointers, but it is horrible. I couldn't avoid using

How should I correctly assign cout to a static ostream reference variable?

会有一股神秘感。 提交于 2019-12-09 18:27:39
问题 I'm defining a class like this: class StaticRuntimeContext { public: enum Verbosity { kHIGH, kMEDIUM, kLOW, kSILENT }; static void Construct(); static std::ostream& stdout1() {return stdout1_;} static std::ostream& stdout2() {return stdout2_;} static std::ostream& stdout3() {return stdout3_;} static std::ostream& stderr() {return stderr_;} protected: private: static std::ostream& stdout1_; static std::ostream& stdout2_; static std::ostream& stdout3_; static std::ostream& stderr_; }; I'm

C++ Passing ostream as parameter

别说谁变了你拦得住时间么 提交于 2019-12-09 10:27:05
问题 I'm working on a homework project for a virtual rolodex that has called for a main class, a rolodex class, and a card class. To output the contents of all of the "cards" to the console, the assignment says that main() should call a show(...) function in the rolodex class, passing it an ostream and show(...) then iterates over the cards, calling each of their showCard() functions. The actual showing is done by the card objects' showCard() function, showing on the provided ostream. What I don't

<< Operator Rewrite to cout int and double values

坚强是说给别人听的谎言 提交于 2019-12-09 03:48:51
问题 I need to rewrite the << operator so that it can cout values for hour (int) and temperature (double). I think I've included all necessary sections. Thanks in advance. struct Reading { int hour; double temperature; Reading(int h, double t): hour(h), temperature(t) { } bool operator<(const Reading &r) const; }; ======== ostream& operator<<(ostream& ost, const Reading &r) { // unsure what to enter here return ost; } ======== vector<Reading> get_temps() { // stub version cout << "Please enter

Printing an uninitialized bool using cout (C++)

为君一笑 提交于 2019-12-08 16:52:08
问题 I have a class with a bool data member that is not initialized by the constructor. If I do cout << x.myBoolDataMember; where x is an object of this class in which the bool has not been initialized, I sometimes get a random number rather than 0 or 1. (I'm using gcc .) Is this behavior compliant with the Standard ? 回答1: Is this behavior compliant with the standard? Yes! Using garbage values(uninitialized) in your code invokes Undefined Behavior 回答2: Yes. An uninitialized variable can have any

istream >> ostream << Operator Overloading with * Pointer

我只是一个虾纸丫 提交于 2019-12-08 01:46:24
问题 How would I overload the >> and << operators if they are dealing with pointers? in header: friend std::istream& operator >>( std::istream& ins, Classname* & e); friend std::ostream& operator <<( std::ostream& outs, const Classname * e); in cpp: std::ostream& operator <<( std::ostream& outs, const Classname * e) { // what do I do here? return outs; } std::istream& operator >>( std::istream& ins, Classname* & e){ // what do I do here? return ins; } 回答1: It depends on what is in the class