istream

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

How to create C++ istringstream from a char array with null(0) characters?

别等时光非礼了梦想. 提交于 2019-12-06 05:11:54
问题 I have a char array which contains null characters at random locations. I tried to create an iStringStream using this array (encodedData_arr) as below, I use this iStringStream to insert binary data(imagedata of Iplimage) to a MySQL database blob field(using MySQL Connector/C++'s setBlob(istream *is) ) it only stores the characters upto the first null character. Is there a way to create an iStringStream using a char array with null characters? unsigned char *encodedData_arr = new unsigned

C++: strange behavior with std::istream or sentry wrap around

微笑、不失礼 提交于 2019-12-06 04:57:21
This small custom getline function was given as an answer to a question about handling different line endings. The function worked great until it was edited 2 days ago to make it not skip leading white spaces for each line. However, after the edit, the program now goes into an infinite loop. The only change done to the code was this following line which was changed from this: std::istream::sentry se(is); // When this line is enabled, the program executes // correctly (no infinite loop) but it does skip // leading white spaces to this: std::istream::sentry se(is, true); // With this line

How to implement custom std::streambuf's seekoff()?

烈酒焚心 提交于 2019-12-06 04:37:18
问题 I have the following implementation based on e.g. this question and answer struct membuf : std::streambuf { membuf(char* begin, char* end) { this->setg(begin, begin, end); } protected: virtual pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which = std::ios_base::in) { std::istream::pos_type ret; if(dir == std::ios_base::cur) { this->gbump(off); } // something is missing here... } }; I would like to use it in my methods in the following way: char buffer[] =

istream extraction operator: how to detect parse failure?

∥☆過路亽.° 提交于 2019-12-05 11:46:17
How can I detect whether the istream extraction failed like this? string s("x"); stringstream ss(s); int i; ss >> std::ios::hex >> i; EDIT -- Though the question title covers this, I forgot to mention in the body: I really want to detect whether the failure is due to bad formatting, i.e. parsing, or due to any other IO-related issue, in order to provide proper feedback (an malformed_exception("x") or whatever). if(! (ss >> std::ios::hex >> i) ) { std::cerr << "stream extraction failed!" << std::endl; } It's just that easy. ETA: Here's an example of how this test interacts with the end of a

istream::tellg() returns -1 when used with my custom streambuf class?

♀尐吖头ヾ 提交于 2019-12-05 09:09:34
I'm trying to create an istream that reads directly from a raw memory buffer. I found a nice way to do this in another post on here: class membuf : public basic_streambuf<char> { public: membuf(char* p, size_t n) { setg(p, p, p + n); } }; Then I create my istream using this membuf : membuf mb(dataPointer, dataLength); istream reader(&mb); I then read using getline() and >> operators, and everything is wonderful. However, I can't seem to use seekg() to rewind back to the beginning of my buffer, and istream::tellg() always returns -1 . Do I need to write some more code to get these to work, or

How do I implement seekg() for a custom istream/streambuf?

寵の児 提交于 2019-12-05 06:04:30
I used to be a C++ expert a decade ago, but for the past 10 years I've been programming Java. I just started a C++ project that uses a small third-party XML parser. The XML parser accepts an STL istream. My XML data is coming from a Windows COM IStream. I thought I'd do the Right Thing and create an adapter to take the IStream data and present it to the XML parser through an istream. I followed the excellent tutorial at http://www.mr-edd.co.uk/blog/beginners_guide_streambuf and created a COMStreambuf that takes data from the underlying COM IStream, and used it as a buffer for a custom

istream eof discrepancy between libc++ and libstdc++

空扰寡人 提交于 2019-12-05 03:10:30
The following (toy) program returns different things when linked against libstdc++ and libc++. Is this a bug in libc++ or do I not understand how istream eof() works? I have tried running it using g++ on linux and mac os x and clang on mac os x, with and without -std=c++0x. It was my impression that eof() does not return true until an attempt to read (by get() or something else) actually fails. This is how libstdc++ behaves, but not how libc++ behaves. #include <iostream> #include <sstream> int main() { std::stringstream s; s << "a"; std::cout << "EOF? " << (s.eof() ? "T" : "F") << std::endl;

non-copying istringstream

匆匆过客 提交于 2019-12-05 01:15:27
So istringstream copies the contents of a string when initialised, e.g string moo("one two three four"); istringstream iss(moo.c_str()); I was wondering if there's a way to make std::istringstream use the given c_str as its buffer without copying things. This way, it won't have to copy large bits of memory before passing the std::istringstream& to functions that take istream& as an argument. What I've been trying to do is convert some functions which only take std::ifstream& arguments (they're mostly parsers) into taking istream& as well. Would I have to make my own istream subclass for this?

How to create C++ istringstream from a char array with null(0) characters?

做~自己de王妃 提交于 2019-12-04 11:27:20
I have a char array which contains null characters at random locations. I tried to create an iStringStream using this array (encodedData_arr) as below, I use this iStringStream to insert binary data(imagedata of Iplimage) to a MySQL database blob field(using MySQL Connector/C++'s setBlob(istream *is) ) it only stores the characters upto the first null character. Is there a way to create an iStringStream using a char array with null characters? unsigned char *encodedData_arr = new unsigned char[data_vector_uchar->size()]; // Assign the data of vector<unsigned char> to the encodedData_arr for