istream

Get an istream from a char*

£可爱£侵袭症+ 提交于 2019-12-27 17:03:20
问题 I have a char* and the data length that I'm receiving from a library, and I need to pass the data to a function that takes an istream. I know I can create a stringstream but that will copy all the data. And also, the data will surely have 0s since it's a zip file, and creating a stringstream will take the data until the first 0 I think. Is there any way to create an istream from a char* and it's size without copying all the data? 回答1: Here's a non-deprecated method found on the web, has you

What did the author mean with his comment about the input of a user-defined type?

南笙酒味 提交于 2019-12-25 16:57:32
问题 This is an example extracted from section 10.3.3 Input of User-defined Types from the book "The C++ Programming Language" second edition, by B. Stroustrup. The code is old but still compiles with minor changes. Example: #include <istream> #include <complex> using namespace std; istream& operator>>(istream& s, complex<double>& a) { // input formats for a complex; "f" indicates a float: // // f // (f) // (f, f) double re = 0, im = 0; char c = 0; s >> c; if( c == '(' ) { s >> re >> c; if( c == '

C++: Reading Data and Outputting Data

喜你入骨 提交于 2019-12-25 15:52:13
问题 I'm attempting to write two methods. One, ReadData(istream&) to read in student's ID number, first and last names, 10 program scores, and midterm and exam scores(last two integers) and returns true if all data was read successfully, false otherwise, and one, WriteData(ostream&) to write the data that was read in to a new file in the same order listed above. I am completely new to file reading and writing so any and all help is much appreciated. The data I am using looks like this...(made up

C++: Did getline() read entire line?

混江龙づ霸主 提交于 2019-12-24 07:17:15
问题 How can I verify that std::istream::getline() reached the delimiter, instead of just max-ing out the input buffer? I realize that I can use gcount() to determine whether fewer bytes than the buffer holds were read, but what if the line to read is exactly as long as the buffer? Reaching \n would look exactly the same as not reaching \n . 回答1: The conditions for terminating std::istream::getline() are quite precise: If the function stops because n - 1 were stored but no newline was reached, the

boost::lexical_cast not recognizing overloaded istream operator

好久不见. 提交于 2019-12-23 09:39:12
问题 I have the following code: #include <iostream> #include <boost\lexical_cast.hpp> struct vec2_t { float x; float y; }; std::istream& operator>>(std::istream& istream, vec2_t& v) { istream >> v.x >> v.y; return istream; } int main() { auto v = boost::lexical_cast<vec2_t>("1231.2 152.9"); std::cout << v.x << " " << v.y; return 0; } I am receiving the following compile error from Boost: Error 1 error C2338: Target type is neither std::istream able nor std::wistream able This seems straightforward

Remove whitespace from input stream by only means of istream functions

六月ゝ 毕业季﹏ 提交于 2019-12-23 05:29:34
问题 Is there any way of to remove the trailing whitespace after entered a decimal? E.g.: 10 A I want to catch the first character after the whitespace ends. (Which gotta be \n to be true. if not, then false My attempt so far: cout << "Please enter a number: "; cin >> n; if (cin.peek() == ' ') //Something to catch the whitespaces if(cin.fail() || cin.peek() != '\n') cout << "Not a number." << endl; else cout << "A number." << endl; Possible to do that by functions in istream? (I know cin.fail can

Unexpected behaviour of getline() with ifstream

為{幸葍}努か 提交于 2019-12-22 18:24:56
问题 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; /

boost read_until does not stop at delimiter

≡放荡痞女 提交于 2019-12-22 10:42:58
问题 I'm using the boost read_until function to facilitate receiving and parsing HTTP messages over a socket. So what I'm trying to do is read_until from the socket until \r\n , which I think should give me one line of the HTTP header. (Each HTTP header line ends in \r\n , per the standard.) However, what I'm actually getting from read_line instead is the entire header, several lines long. (The header ends in \r\n\r\n , or in other words, a blank line. Also, per the HTTP standard.) Here's a code

Why `s.clear(ios::badbit);` below? Why not `s.clear(ios::failbit);`?

落爺英雄遲暮 提交于 2019-12-22 08:34:29
问题 I was looking into this question and I have no problem understanding the two answers given to it. But I'm not sure I understood the s.clear(ios::badbit); in the statement highlighted below with the comment // set state . For instance, why not s.clear(ios::failbit); instead? #include <istream> #include <complex> using namespace std; istream& operator>>(istream& s, complex<double>& a) { // input formats for a complex; "f" indicates a float: // // f // (f) // (f, f) double re = 0, im = 0; char c

substream from istream

六眼飞鱼酱① 提交于 2019-12-21 18:33:44
问题 Suppose I have an ifstream which represents a large file containing lots of sub-files aggregated together. I want to be able to create a "sub" istream from the larger ifstream (given a size and offest) representing a part of the file so other code can read from that substream as if it was an independent istream . Any ideas on how I might accomplish this? EDIT - I would prefer to avoid boost. 回答1: This is an example of a streambuf "filter" that reads from a contained streambuf starting at a