iostream

Having a stream's streambuf persist beyond stream's destruction?

房东的猫 提交于 2019-12-11 03:34:51
问题 Is it possible to have a stream's streambuf persist after the destruction of its originating stream's destruction? streambuf* f() { ifstream my_stream; // ... return my_stream.rdbuf(); } void g() { streambuf *s = f(); // ... ifstream my_new_stream; my_new_stream.rdbuf( s ); } I.e., I want the pointer to the streambuf object returned by f() to remain valid even after my_stream goes out of scope. At some later time, I want to set some other stream's streambuf to the saved streambuf . Is this

How to compare two std::istream references?

瘦欲@ 提交于 2019-12-11 03:07:41
问题 I am switching over compilers from GCC to Clang/LLVM and running into compilation errors I didn't experience before. I have a class that looks something like this: #include <iostream> class foo { public: bar(std::istream& is) : _fp(is), _sCheck(is != std::cin) { /* ... */ } private: std::istream& _fp; bool _sCheck; } When I compile this file, I get the following error with clang++ , where the initialization of the private variable _sCheck fails: error: invalid operands to binary expression (

Composite std::istream in C++

橙三吉。 提交于 2019-12-11 02:47:55
问题 I have a list of std::istream objects that I need to appear as a single std::istream object. So if I have three istream s, A, B and C, I want to be able to create an istream , D which will first return the bytes from A then the bytes from B, then C before EOF is reached. The composite stream will always be read sequentially and closed after all bytes are read. Is there a simple way to do this using stl/boost or do I just need to write my own composite istream? 回答1: The off-topic answer to

implementing simple input stream

萝らか妹 提交于 2019-12-11 01:34:07
问题 I want to write a simple istream object, that would simply transform another istream . I want to only implement readline (which would read a line from the original stream, would process it, and return the processed line), and have some generic code that upon read would use my read line, cache it, and give the required amount of bytes as output. Is there any class that would allow me to do that? For example struct mystream : istreamByReadLine { istream& s; mystream(istream& _s):s(_s){} virtual

Using iostream << to serialize user objects

倾然丶 夕夏残阳落幕 提交于 2019-12-10 23:59:58
问题 I want serialize object to binary file using operator "<<", but when I serialize, for example, int fields, I obtained it's symbolic representation: ofstream out("file", ios::out | ios::binary); int i=0xAA; out << i; And output: 0x31 0x37 0x30 i.e. (0xAA -> 170) 170 If I use write function, all ok: out.write((char*)&i,sizeof(int)); Output: 0xAA 0x00 0x00 0x00 But can I use << instead write function, to serialize object? Like: out << obj.field1 << obj.field2; // etc. 回答1: First, a warning: You

Is my approach to a threadsafe log class awful?

假装没事ソ 提交于 2019-12-10 19:48:53
问题 I've been looking around at various approaches to the problem of threadsafe logging, but I haven't seen anything quite like this, so I dunno if it's somehow awful that I haven't noticed due to being a complete newbie to C++, threads and iostreams. It seems to work in the basic tests I've put it through. Basically I have a Log class (creative, I know...) which has operator<< set up for the standard manipulators, so I can merrily pass in whatever I want. However, I am aware that something like:

In Java: why some Stream methods take int instead of byte or even char?

℡╲_俬逩灬. 提交于 2019-12-10 18:42:19
问题 Why some methods that write bytes/chars to streams takes int instead of byte/char ?? Someone told me in case of in t instead of char : because char in java is just 2 bytes length, which is OK with most character symbols already in use, but for certain character symbols (chines or whatever), the character is being represented in more than 2 bytes, and hence we use int instead. How far this explanation is close to the truth? EDIT: I use the stream word to represent Binary and character streams

parsing strings with value modifiers ('-', '%') at the end

核能气质少年 提交于 2019-12-10 18:22:21
问题 I try to get to grips with parsing. I have some data that comes in a de-de format with additional information at the end of the string. I managed to get the de-de part correct but I struggle in getting the - and % parsed correctly. I read up on codecvt but I do not understand the topic. Here is a reflection of what I understand so far and an example of what I need to do. #include <string> #include <locale> #include <iostream> #include <sstream> using namespace std; #define EXPECT_EQ(actual,

How to read and write in file with `fstream` simultaneously in c++?

一个人想着一个人 提交于 2019-12-10 17:52:50
问题 I have the following code #include<iostream> #include<fstream> #include<string> using namespace std; int main(void) { fstream ofile; ofile.open("test.txt", ios::in | ios::out | ios::app); for(string line; getline(ofile, line) ; ) { cout << line << endl; } ofile << "stackexchnange" << endl; ofile.close(); return 0; } test.txt contains hello world! stackoverflow Above code outputs hello world! stackoverflow And after running the code stackexchange is not appending at the end of test.txt . How

Can I strip carriage returns as they go into a `std::stringstream`?

非 Y 不嫁゛ 提交于 2019-12-10 17:45:16
问题 struct T { void eat(std::string const& segment) { buffer << segment; std::string sentence; while (std::getline(buffer, sentence)) std::cout << "[" << sentence.size() << "]"; } std::stringstream buffer; }; int main() { T t; t.eat("A\r\nB\nC\nD"); // ^^ ^ ^ ^ } // Actual output: [2][1][1][1] // Desired output: [1][1][1][1] I would like the std::stringstream to strip that carriage return for me (and would prefer not to have to copy and modify segment ). How might I go about this? I would have