istream

Read from cin or a file

三世轮回 提交于 2019-12-01 17:16:21
When I try to compile the code istream in; if (argc==1) in=cin; else { ifstream ifn(argv[1]); in=ifn; } gcc fails, complaining that operator= is private. Is there any way to set an istream to different values based on a condition? You can replace cin's streambuf with another, and in some programs this is simpler than the general strategy of passing around istreams without referring to cin directly. int main(int argc, char* argv[]) { ifstream input; streambuf* orig_cin = 0; if (argc >= 2) { input.open(argv[1]); if (!input) return 1; orig_cin = cin.rdbuf(input.rdbuf()); cin.tie(0); // tied to

Skip lines in std::istream

做~自己de王妃 提交于 2019-12-01 17:13:33
I'm using std::getline() to read lines from an std::istream-derived class, how can I move forward a few lines? Do I have to just read and discard them? Billy ONeal Edit: You can also use std::istream::ignore, see https://stackoverflow.com/a/25012566/492336 Do I have to use getline the number of lines I want to skip? No, but it's probably going to be the clearest solution to those reading your code. If the number of lines you're skipping is large, you can improve performance by reading large blocks and counting newlines in each block, stopping and repositioning the file to the last newline's

Skip lines in std::istream

旧街凉风 提交于 2019-12-01 15:15:16
问题 I'm using std::getline() to read lines from an std::istream-derived class, how can I move forward a few lines? Do I have to just read and discard them? 回答1: Edit: You can also use std::istream::ignore, see https://stackoverflow.com/a/25012566/492336 Do I have to use getline the number of lines I want to skip? No, but it's probably going to be the clearest solution to those reading your code. If the number of lines you're skipping is large, you can improve performance by reading large blocks

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

此生再无相见时 提交于 2019-12-01 12:25:14
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 the idl so it doesn't redeclare IStream (importing MSCOREE does that, but doesn't solve the C# problem)

C++ user input restriction with proper retry without “goto”

拥有回忆 提交于 2019-12-01 11:44:12
I have the following code: qstn: cout << "Input customer's lastname: "; getline(cin, lname); if (lname.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ") != string::npos) { cout << "You can only input alpha here!\n"; cin.clear(); goto qstn; } else if (lname.empty()) { cout << "Please enter your firstname!\n"; cin.clear(); goto qstn; } int lnamel = lname.length(); int strl = str.length(); int is = 0; for (int i = 1; i < strl;) { i++; is++; if (lname[i] == lname[is] && lname[i] == ' ' || lname[0] == ' ') { cin.clear(); cout << "Please input your lastname properly!\n";

Reuse std::cin after eating an EOF

痞子三分冷 提交于 2019-12-01 07:39:44
问题 The unix command wc has this functionality: $ wc - - - aaa bbb ccc<EOF> 0 3 11 - aaa bbb ccc<EOF> 0 3 11 - aaa bbb ccc<EOF> 0 3 11 - 0 9 33 total Each <EOF> indicates a <C-d> key sequence that enters an EOF into stdin. wc is then able to pick up this EOF . I'm trying to implement this in C++. A common suggestion is combination of clear() and ignore() . char c; while (std::cin >> c) { ... } std::cin.clear(); std::cin.ignore(); while (std::cin >> c) { /* never executed */ } I've also tried std:

C++ user input restriction with proper retry without “goto”

你说的曾经没有我的故事 提交于 2019-12-01 07:36:34
问题 I have the following code: qstn: cout << "Input customer's lastname: "; getline(cin, lname); if (lname.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ") != string::npos) { cout << "You can only input alpha here!\n"; cin.clear(); goto qstn; } else if (lname.empty()) { cout << "Please enter your firstname!\n"; cin.clear(); goto qstn; } int lnamel = lname.length(); int strl = str.length(); int is = 0; for (int i = 1; i < strl;) { i++; is++; if (lname[i] == lname[is] &&

Is this a bug with getline(), or am I doing something wrong. Right way to use getline()?

我只是一个虾纸丫 提交于 2019-12-01 06:07:44
It might not be a bug, but I don't know what is going wrong. My first entry is repeated for str1 on 2nd iteration, and is same way from then. Only first iteration goes good. #include <iostream> #include <string> using namespace std; int main () { cout << " \n Enter two words. \n " ; char c = 'y'; string str; string str1; while (c == 'y'){ getline(cin,str); getline (cin,str1); cout << " \n\n str : " << str << " str1 : " << str1 ; cout << " \n Continue ? \n " ; cin >> c; } return 0; } The output is : Enter two words. hello world this is mr str : hello world str1 : this is mr Continue ? y hello

Is this a bug with getline(), or am I doing something wrong. Right way to use getline()?

自古美人都是妖i 提交于 2019-12-01 03:56:39
问题 It might not be a bug, but I don't know what is going wrong. My first entry is repeated for str1 on 2nd iteration, and is same way from then. Only first iteration goes good. #include <iostream> #include <string> using namespace std; int main () { cout << " \n Enter two words. \n " ; char c = 'y'; string str; string str1; while (c == 'y'){ getline(cin,str); getline (cin,str1); cout << " \n\n str : " << str << " str1 : " << str1 ; cout << " \n Continue ? \n " ; cin >> c; } return 0; } The

How to store formatting settings with an IOStream?

蹲街弑〆低调 提交于 2019-11-30 19:27:33
When creating formatted output for a user defined type it is often desirable to define custom formatting flags. For example, it would be nice if a custom string class could optionally add quotes around the string: String str("example"); std::cout << str << ' ' << squotes << str << << ' ' << dquotes << str << '\n'; should produce example 'example' "example" It is easy enough to create manipulators for changing the formatting flags themselves: std::ostream& squotes(std::ostream& out) { // what magic goes here? return out; } std::ostream& dquotes(std::ostream& out) { // similar magic as above