iostream

Output UTF-8 (u8) std::string

老子叫甜甜 提交于 2020-01-06 04:29:05
问题 In C++11 and later, using the u8 prefix on a string literal can create char (byte) sequences that are UTF-8 encoded. How do you output those sequences to a std::ostream ? How do you tell a std::ostream that a const char * or std:string to be output contains characters encoded in UTF-8, rather than the default encoding? 回答1: You don't. The stream does not know or care what the encoding of the text is. Despite it's name, a char is not treated by std:ostream as containing a character encoded in

Writing multiple array pointers to file with ofstream?

╄→гoц情女王★ 提交于 2020-01-06 04:25:52
问题 I'm having some seriously strange trouble writing multiple arrays of data to a file. Basically, I'm wanting to store all the array sizes at the top of the file, and then the array data following. This way I can just read the sizes and use that to construct arrays to hold the data on import, and I'll know exactly where each array begins and ends. Here's the problem: I write the data, but it's different on import. Please take a look at my little test code. At the bottom there are comments about

Redirect debug output to null stream instead of std::cerr

别等时光非礼了梦想. 提交于 2020-01-04 03:47:52
问题 A software library that I am working with writes a lot of debug output to std::cerr , but redirects that output to a null stream if I tell it to be quiet. This is a simplified main.cpp that shows how the code tries to achieve this: #include <iostream> #include <fstream> #include <cassert> // The stream that debug output is sent to. By default // this points to std::cerr. std::ostream* debugStream(&std::cerr); // Throughout the library's codebase this function is called // to get the stream

Redirect debug output to null stream instead of std::cerr

北慕城南 提交于 2020-01-04 03:47:12
问题 A software library that I am working with writes a lot of debug output to std::cerr , but redirects that output to a null stream if I tell it to be quiet. This is a simplified main.cpp that shows how the code tries to achieve this: #include <iostream> #include <fstream> #include <cassert> // The stream that debug output is sent to. By default // this points to std::cerr. std::ostream* debugStream(&std::cerr); // Throughout the library's codebase this function is called // to get the stream

How to read enums from a std::istream in a generic fashion [duplicate]

时光总嘲笑我的痴心妄想 提交于 2020-01-03 19:51:20
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Input from stream to enum type I have several classes with different enums as class members and I want to read the classes from a stream. The following code shows an exemplary class: enum enSide{ eLeft, eRight }; enum enType{ eConUndefined, eConRoom }; class MyClass{ public: friend std::istream& operator>>(std::istream& in, MyClass& val) { in >> val.mSide >> val.mType >> val.mTargetId; return in; } MyClass(){}

Why does istream_iterator<string>(ifstream(“test.txt”)) cause an error?

喜你入骨 提交于 2020-01-03 17:08:23
问题 I have tried to write a code to read strings from file named "test.txt" and write the strings to standard output. The code below works well: int main() { using namespace std; ifstream file("test.txt"); copy(istream_iterator<string>(file), istream_iterator<string>(), ostream_iterator<string>(cout, " ")); } However, with this modification, the code no longer compiles: int main() { using namespace std; copy(istream_iterator<string>(ifstream("test.txt")), // <-- Error here istream_iterator<string

Is there a simple way to get the number of characters printed in C++?

我的未来我决定 提交于 2020-01-03 08:37:12
问题 printf(...) returns the number of characters output to the console, which I find very helpful in designing certain programs. So, I was wondering if there is a similar feature in C++, since the cout<< is an operator without a return type (at least from what I understand of it). 回答1: You can associate your own streambuf to cout to count the characters. This is the class that wraps it all: class CCountChars { public: CCountChars(ostream &s1) : m_s1(s1), m_buf(s1.rdbuf()), m_s1OrigBuf(s1.rdbuf(&m

Is there a simple way to get the number of characters printed in C++?

我的未来我决定 提交于 2020-01-03 08:37:08
问题 printf(...) returns the number of characters output to the console, which I find very helpful in designing certain programs. So, I was wondering if there is a similar feature in C++, since the cout<< is an operator without a return type (at least from what I understand of it). 回答1: You can associate your own streambuf to cout to count the characters. This is the class that wraps it all: class CCountChars { public: CCountChars(ostream &s1) : m_s1(s1), m_buf(s1.rdbuf()), m_s1OrigBuf(s1.rdbuf(&m

Why does not seekg(0) clear the eof state of stream?

冷暖自知 提交于 2020-01-03 08:30:57
问题 I would like to know if and why seekg(0) is not supposed to clear the eofbit of a stream. I am in a point where I have already read all the stream, thus EOF has been reached (but no failbit is set yet) and want to go back with seekg() to a valid position and read some chars again. In this case seekg(0) seems "to work" with the eofbit set, but as soon as I try to read from the stream, the failbit is set. Is this logic, correct or is my implementation bad? Am I supposed to recognize this case

using stream operator<< with std::endl in c++

时间秒杀一切 提交于 2020-01-02 07:30:30
问题 I am trying out the following C++ class for using the stream operator << to log contents from this answer: class Log { public: Log() : m_filename( "dafault.log" ) {} // if you wanna give other names eventually... Log( const std::string & p_filename ) : m_filename( p_filename ) {} virtual ~Log() { // implement your writeToFile() with std::ofstream writeToFile( m_filename, m_stream, true ); } template< typename T > Log & operator<<( const T & p_value ) { m_stream << p_value; return *this; }