istream

istringstream not honoring base?

核能气质少年 提交于 2019-12-11 10:30:08
问题 I'm trying to remediate some Coverity findings on tainted values due to the use of atoi and atof . I switched to an istringstream , but its not producing expected results for bases other than 10. If I switch to base 16, enter 0xa and avoid the iss.ignore(2); , then the result is 0: $ ./tt.exe 0xa X: 0 If I switch to base 16, enter 0xa and utilize the iss.ignore(2); , then the result is an exception: $ ./tt.exe 0xa '0xa' is not a value I visited CPP Reference on istringstream as recommended by

Operator Overloading: Ostream/Istream

萝らか妹 提交于 2019-12-11 07:23:59
问题 I'm having a bit of trouble with a lab assignment for my C++ class. Basically, I'm trying to get the "cout << w3 << endl;" to work, so that when I run the program the console says "16". I've figured out that I need to use an ostream overload operation but I have no idea where to put it or how to use it, because my professor never spoke about it. Unfortunately, I HAVE to use the format "cout << w3" and not "cout << w3.num". The latter would be so much quicker and easier, I know, but that's not

Moving a class derived from istream

拥有回忆 提交于 2019-12-11 05:29:12
问题 I'm creating a C++ istream with a custom streambuf . Trying to move this fails because the istream move constructor is protected. To get round this I derived a class from istream : struct VectorCharBuf : public streambuf { VectorCharBuf(vector<char>& v) { setg(v.data(), v.data(), v.data() + v.size()); } }; struct IVectorCharStream : public istream { IVectorCharStream(VectorCharBuf* contents_buf) : istream(contents_buf) {} }; The default move constructor for this class is not generated because

C++: std::istream read and its calls to std::streambuf underflow

会有一股神秘感。 提交于 2019-12-11 03:55:01
问题 the following code simply tests how often underflow is called when using std::istream read on an std::stringbuf . #include <iostream> #include <vector> #include <sstream> class TestStringBuf : public std::stringbuf { public: int_type underflow() { std::cout<<"TestStringBuf underflow"<<std::endl; return std::stringbuf::underflow(); } }; int main(int argc, const char * argv[]) { TestStringBuf buf; std::iostream stream(&buf); stream << "tesr"; std::vector<char> data(4); stream.read(&data[0], 4);

Is there a Way to Stream in a Map?

怎甘沉沦 提交于 2019-12-10 18:03:26
问题 I have a file with map entries separated by line, and the keys and values separated by a ':' So something like: one : 1 two : 2 three:3 four : 4 I open this in an ifstream called dict and I run the following code: string key, value; map< string, int > mytest; while( getline( dict, key, ':' ).good() && getline( dict, value ).good() ) { mytest[key] = atoi( value.c_str() ); } Is there a better way to do this? Is there a getline functionality that would strip spaces from the key? (I'm trying to

How to block on reading a c++ stringstream to wait for data

你。 提交于 2019-12-10 17:35:53
问题 So, I've been trying to figure out, how to wait for data from a C++ stringstream (for instance), without being constantly checking if data is there, which is quite CPU consuming. I'm perfectly able to read, for instance, from a serial device, and lock the process while no data arrives, but unfortunately I haven't been able to figure how to do that with C++ streams. I'm sure I'm missing something, since cin does exactly that, i.e., waits for the return key to step out from istream reading, but

insert a string in front of a istream in cpp

一笑奈何 提交于 2019-12-10 17:01:28
问题 My problem is something like I want to append some string in front of a iostream. You can say in front of std::cin. #include <iostream> #include <string> void print(std::istream & in){// function not to be modified std::string str; in >> str; std::cout<< str << std::endl; in >> str; std::cout<< str << std::endl; } int main() { std::string header = "hello "; //boost::iostream::filtering_istream in(std::cin); std::istream & in = std::cin; //you may do someting here //something like inserting

Input stream iterators and exceptions

冷暖自知 提交于 2019-12-10 16:11:12
问题 I was playing around with istream iterators and exception handling a few days ago and I came across with this curiousity: #include <iostream> #include <fstream> #include <iterator> #include <algorithm> using namespace std; int main(int argc, char* argv[]) { if (argc < 2) { cout << argv[0] << " <file>" << endl; return -1; } try { ifstream ifs(argv[1]); ifs.exceptions(ios::failbit | ios::badbit); istream_iterator<string> iss(ifs), iss_end; copy(iss, iss_end, ostream_iterator<string>(cout, "\n")

“carbon-copy” a c++ istream?

别来无恙 提交于 2019-12-10 15:40:14
问题 For my very own little parser framework, I am trying to define (something like) the following function: template <class T> // with operator>>( std::istream&, T& ) void tryParse( std::istream& is, T& tgt ) { is >> tgt /* , *BUT* store every character that is consumed by this operation in some string. If afterwards, is.fail() (which should indicate a parsing error for now), put all the characters read back into the 'is' stream so that we can try a different parser. */ } Then I could write

Get exact size of IPv6 header including the extenstion headers

痴心易碎 提交于 2019-12-10 12:08:21
问题 If IPv4 is in question and I want to extract IP and ICMP header out of std::istream, first I get the initial 20 bytes, then check if the header lenght provided in the IPv4 header is larger than 20 bytes in order to extract any options. The next bytes are the ICMP packet. Using the header lenght value carried inside the IPv4 header I can see the expect size of the IP header. However how to get the exact size of IPv6 header? There is a payload value inside IPv6 header that includes the size of