C++ Streams vs. C-style IO?

This is an heated topic. Some people prefer to use the C++ IO since they are type-safe (you can’t have divergence between the type of the object and the type specified in the format string), and flow more naturally with the rest of the C++ way of coding. However, there is also arguments for C … Read more

Colored output in C++

You need the terminal color codes. For linux it’s the following (your system might be different, look it up): //the following are UBUNTU/LINUX, and MacOS ONLY terminal color codes. #define RESET “\033[0m” #define BLACK “\033[30m” /* Black */ #define RED “\033[31m” /* Red */ #define GREEN “\033[32m” /* Green */ #define YELLOW “\033[33m” /* Yellow … Read more

Why is std::streamsize defined as signed rather than unsigned?

The draft C++ standard has the following footnote 296 in section 27.5.2 Types which says: streamsize is used in most places where ISO C would use size_t. Most of the uses of streamsize could use size_t, except for the strstreambuf constructors, which require negative values. It should probably be the signed type corresponding to size_t … Read more

Unexpected results with std::ofstream binary write

std::ios::binary promises to not do any line-end conversions on the stream (and some other small behavioral differences with text streams). You could look at Boost Serialization http://www.boost.org/doc/libs/1_53_0/libs/serialization/doc/index.html Boost Spirit binary generators http://www.boost.org/doc/libs/1_53_0/libs/spirit/doc/html/spirit/karma/reference/binary/ Using ofstream::write(…) to manually write the bytes Here’s an example using Boost Spirit Karma (assuming Big-Endian byte ordering): #include <boost/spirit/include/karma.hpp> namespace karma = … Read more

cstdio streams vs iostream streams?

The C and C++ standards make no requirements on how things are implemented, just on what the effect of certain operations is. For the <stdio> vs. <iostream> functionality this means that one could wrap the other, both could be essentially the same, or that they are either entirely independent. Technically, using a common implementation would … Read more

How can I properly overload the

I am just telling you about one other possibility: I like using friend definitions for that: namespace Math { class Matrix { public: […] friend std::ostream& operator<< (std::ostream& stream, const Matrix& matrix) { […] } }; } The function will be automatically targeted into the surrounding namespace Math (even though its definition appears within the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)