ostream

Can I flush data implicitly?

老子叫甜甜 提交于 2019-12-07 18:56:46
问题 Is there a way to implicitly flush data to an output stream? #include <iostream> #include <fstream> using namespace std; #define log logstream int main() { ofstream logstream("test.log"); log << "Test1" << 123 << endl; // explicitly flushed log << "Test2" << 123; // ? // Test2 not written, yet... cout << "Check log file..." << endl; int tmp; cin >> tmp; } I would like to be able to log without specifying the << endl manipulator every time. 回答1: You may use std::unitbuf. log << std::unitbuf;

C++11 Adding a stream output operator for std::chrono::time_point

孤者浪人 提交于 2019-12-07 18:23:42
问题 I would like to be able to do the following: std::cerr << std::chrono::system_clock::now() << std::endl; And get the following: Wed May 1 11:11:12 2013 So I wrote the following: template<typename Clock, typename Duration> std::ostream &operator<<(std::ostream &stream, const std::chrono::time_point<Clock, Duration> &time_point) { const time_t time = Clock::to_time_t(time_point); #if __GNUC__ > 4 || \ ((__GNUC__ == 4) && __GNUC_MINOR__ > 8 && __GNUC_REVISION__ > 1) // Maybe the put_time will be

Can cout throw an exception?

霸气de小男生 提交于 2019-12-07 17:45:28
问题 cout is an instance of type "ostream" ostream::operator<< says that If the operation sets an internal state flag that was registered with member exceptions, the function throws an exception of member type failure. This indicates to me that cout can throw an exception. Is this true? What kind of scenario could force this? 回答1: Yes, but practically nobody ever calls cout.exceptions(iostate) to enable that. Edit to expound: std::ios_base is an abstract class that provides basic utility functions

Segmentation fault (core dumped) Error

删除回忆录丶 提交于 2019-12-06 13:18:43
问题 My program compiles fines but upon inputting a file I get a "Segmentation fault (core dumped)" error. Am I not handling the ostream correctly? #include <std_lib_facilities.h> struct Reading { int hour; double temperature; Reading(int h, double t): hour(h), temperature(t) { } bool operator<(const Reading &r) const; }; bool Reading::operator<(const Reading &r) const { // stub version if (temperature < r.temperature){ return true; } else if (r.temperature < temperature) { return false; } } /* *

istream >> ostream << Operator Overloading with * Pointer

浪尽此生 提交于 2019-12-06 05:26:54
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; } It depends on what is in the class Classname . If for example you have: class Classname { //... private: int a; }; .. then you might do: std::ostream

Are std::showbase and std::showpos mutually exclusive?

吃可爱长大的小学妹 提交于 2019-12-06 03:58:18
问题 This question arose from a discussion I was having about the correct way to output a numeric value using the usual ostream & operator << (ostream &, some_type) for a numeric type in C++. The way I'm familiar with the behavior of std::showbase and std::showpos in each base, they are basically mutually exclusive. That is: in decimal no base is shown, and the '+' is added on positive numbers; whereas in hexadecimal or octal, the base is shown, but a '+' is not shown (nor is a minus), as the

inheriting ostream and streambuf problem with xsputn and overflow

∥☆過路亽.° 提交于 2019-12-06 02:47:44
问题 I have been doing research on creating my own ostream and along with that a streambuf to handle the buffer for my ostream. I actually have most of it working, I can insert (<<) into my stream and get strings no problem. I do this by implimenting the virtual function xsputn. However if I input (<<) a float or an int to the stream instead of a string xsputn never gets called. I have walked through the code and I see that the stream is calling do_put, then f_put which eventually tries to put the

Overloaded ostream operator segmentation fault if no endl

时间秒杀一切 提交于 2019-12-06 01:41:38
问题 class foo { public: friend ostream& operator << (ostream &os, const foo &f); foo(int n) : a(n) {} private: vector <int> a; }; ostream& operator << (ostream &os, const foo &f) { for (int i = 0; i < f.a.size(); ++i) os << f.a[i] << " "; os << endl; // why is this line a must? } int main(void) { foo f(2); cout << f << endl; return 0; } In the above code, if the marked line is removed, there will be a segment fault error, can someone explain why? 回答1: ostream& operator << (ostream &os, const foo

Class to output to several ostreams (file and console)

守給你的承諾、 提交于 2019-12-06 01:00:15
Right, I'm not even sure how to correctly formulate this; I feel it's a bit of an involved question. I'm sure someone can help me out here though. This is what I want to do: Have a single class that I can send stuff to, like this. icl << "Blah blah blah" << std::endl; I want to be able to .attach() classes that inherit std::basic_ostream to it. Those classes would then be able to format the output their own way. One might add a timestamp and write to a log, the other might write it to the console, the other might display it in-game. Anyone care to get me started in the right direction? Here's

How to save `std::vector<uchar>` into `std::ostream`?

自古美人都是妖i 提交于 2019-12-06 00:46:34
问题 We have created and filled some std::vector<uchar> with openCV imencode for example. Now we want to stream it for example into some http_lib which can take some sort of ostream (ostringstream) for example, or we just want to save in while we debug our programm with ofstream. So I wonder how to put std::vector<uchar> into std::ostream ? 回答1: Use write: void send_data(std::ostream & o, const std::vector<uchar> & v) { o.write(reinterpret_cast<const char*>(v.data()), v.size()); } The ostream