istream

istream eof discrepancy between libc++ and libstdc++

为君一笑 提交于 2019-12-10 02:56:45
问题 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>

istream::peek curious behavior wrt. EOF

依然范特西╮ 提交于 2019-12-08 02:07:29
问题 I've just encountered a curious situation in C++. I was doing something like: istream in; // ... in.get(); // get a char (which turns out to be the last) // curiously ios::eof bit doesn't get set just yet c = in.peek(); // attempt to peek, return EOF and now set ios::eof bit if(c == EOF) { // realize shouldn't have gotten the last char for some reason in.unget(): // attempt to unget, *fails* (because ios:eof bit was set) } Now I'm curious why peek sets the eof bit; I find this highly

istream >> ostream << Operator Overloading with * Pointer

我只是一个虾纸丫 提交于 2019-12-08 01:46:24
问题 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; } 回答1: It depends on what is in the class

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

自古美人都是妖i 提交于 2019-12-07 14:06:12
问题 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

istream extraction operator: how to detect parse failure?

匆匆过客 提交于 2019-12-07 07:39:23
问题 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). 回答1: if(! (ss >> std::ios::hex >> i) ) { std::cerr << "stream extraction failed!"

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

五迷三道 提交于 2019-12-07 05:46:35
问题 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

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

一个人想着一个人 提交于 2019-12-07 04:07:06
问题 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

C++: how is istream is converted to bool inside a conditional expression [duplicate]

不羁的心 提交于 2019-12-06 14:21:32
问题 This question already has answers here : Why istream object can be used as a bool expression? (2 answers) Closed 6 years ago . The istream operator>> is used to read the data and the function returns reference to istream. For example, istream& operator>> (bool& val); But how does the istream is converted into a bool when it is used inside the conditional statement. For example, ifstream ifs(.....); // open the file istream &is = (istream&)ifs; char c; if(is >> c) // how the istream is been

Unexpected behaviour of getline() with ifstream

我的梦境 提交于 2019-12-06 13:01:50
To simplify, I'm trying to read the content of a CSV-file using the ifstream class and its getline() member function. Here is this CSV-file: 1,2,3 4,5,6 And the code: #include <iostream> #include <typeinfo> #include <fstream> using namespace std; int main() { char csvLoc[] = "/the_CSV_file_localization/"; ifstream csvFile; csvFile.open(csvLoc, ifstream::in); char pStock[5]; //we use a 5-char array just to get rid of unexpected //size problems, even though each number is of size 1 int i =1; //this will be helpful for the diagnostic while(csvFile.eof() == 0) { csvFile.getline(pStock,5,','); cout

istream::peek curious behavior wrt. EOF

試著忘記壹切 提交于 2019-12-06 06:18:27
I've just encountered a curious situation in C++. I was doing something like: istream in; // ... in.get(); // get a char (which turns out to be the last) // curiously ios::eof bit doesn't get set just yet c = in.peek(); // attempt to peek, return EOF and now set ios::eof bit if(c == EOF) { // realize shouldn't have gotten the last char for some reason in.unget(): // attempt to unget, *fails* (because ios:eof bit was set) } Now I'm curious why peek sets the eof bit; I find this highly unintuitive. It's supposed to just peek not actually consume anything and shouldn't change the stream state.