istream

Why does std::getline() skip input after a formatted extraction?

落花浮王杯 提交于 2020-01-28 10:16:40
问题 I have the following piece of code that prompts the user for their name and state: #include <iostream> #include <string> int main() { std::string name; std::string state; if (std::cin >> name && std::getline(std::cin, state)) { std::cout << "Your name is " << name << " and you live in " << state; } } What I find is that the name has been successfully extracted, but not the state. Here is the input and resulting output: Input: "John" "New Hampshire" Output: "Your name is John and you live in "

Why is istream.clear() removing part of my strings while reading doubles and strings?

一曲冷凌霜 提交于 2020-01-24 23:58:08
问题 I am reading a text file consisting of names and grades with the format: Polly 95 99 95 94 93 98 99 Abraham 80 85 81 86 95 78 81 I use the following code to read each student into a Student_info struct: // read homework grades from an input stream into a vector<double> istream& read_hw(istream& in, vector<double>& hw) { if (in) { // get rid of previous content hw.clear(); // read hw grades double grade; while (in >> grade) hw.push_back(grade); // clear the stream so that input will work for

C++ std::istream readsome doesn't read anything

随声附和 提交于 2020-01-23 05:52:13
问题 It's like readsome isn't even reading. Returns 0 and doesn't read any chars. What is wrong here? #include <fstream> #include <iostream> int main () { std::fstream stream("list.cpp", std::ios::in); if (stream.good() || !stream.bad() || stream.is_open()) { std::cout << "Well, stream looks good." << std::endl; char justOneChar = 'L'; auto ssize = stream.readsome(&justOneChar, 1); std::cout << ssize << " : " << justOneChar << std::endl; } return -1; } Output: Well, stream looks good. 0 : L 回答1:

non-copying istringstream

丶灬走出姿态 提交于 2020-01-23 04:46:11
问题 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

Reading multiple data types on one line using C++

可紊 提交于 2020-01-03 06:30:07
问题 I am attempting to pull from a dat file a date composed of ints as well as a char and a float value that are together. Dat file format looks like: 201205171200 M29.65 201207041900 F30.3 And so on. I am struggling with separating these values. Here is what I have so far: #include <iostream> #include <fstream> #include <vector> using namespace std; int main() { int inCount = 0; //This variable will be used to keep track of what record is being read. vector<int> dates; vector<float> temps; //

download a file using windows IStream

穿精又带淫゛_ 提交于 2020-01-01 03:38:06
问题 I'm implementing dragging a virtual file out of a website and onto the desktop with an activex control. How do I create an IStream on my http url, so Windows can execute the drop? The example I'm looking at uses SHCreateStreamOnFile to copy a local file; there must be an analogous function for other types of streams like http file download. 回答1: How about using the URL Moniker Functions. You can use the URLDownloadToFile (blocking function), or URLOpenPullStream (asynchronous). 来源: https:/

istream reading after failure

浪子不回头ぞ 提交于 2019-12-31 07:03:47
问题 I have a small piece of code to read user data, like below: #include<iostream> #include<vector> #include<algorithm> #include<ios> #include<iomanip> #include<string> using namespace std; istream& read_dt(istream& in, vector<int>& nmbr) { if(in) { nmbr.clear(); int x; while(in >> x) { nmbr.push_back(x); } if(in.fail()) { string s; in >> s; in.clear(); } } return in; } int main() { vector<int> data; cout << "Enter all the data: " << endl; read_dt(cin, data); cout << "Size: " << data.size() <<

How do I declare an IStream in idl so visual studio maps it to s.w.interop.comtypes?

狂风中的少年 提交于 2019-12-30 12:20:31
问题 I have a COM object that takes needs to take a stream from a C# client and processes it. It would appear that I should use IStream. So I write my idl like below. Then I use MIDL to compile to a tlb, and compile up my solution, register it, and then add a reference to my library to a C# project. Visual Studio creates an IStream definition in my own library. How can I stop it from doing that, and get it to use the COMTypes IStream? It seems there would be one of 3 answers: add some import to

How to detect empty lines while reading from istream object in C++?

拟墨画扇 提交于 2019-12-30 02:09:07
问题 How can I detect if a line is empty? I have: 1 2 3 4 5 I'm reading this with istream r so: int n; r >> n I want to know when I reach the space between 4 and 5. I tried reading as char and using .peek() to detect \n but this detects the \n that goes after number 1 . The translation of the above input is: 1\n2\n3\n4\n\n5\n if I'm correct... Since I'm going to manipulate the ints I rather read them as ints than using getline and then converting to int... 回答1: It could look like this: #include

Get an istream from a char*

杀马特。学长 韩版系。学妹 提交于 2019-12-27 17:03:56
问题 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