istream

Switching from formatted to unformatted input in C++

可紊 提交于 2019-12-20 07:26:16
问题 I have an input text file. The first line has two int numbers a and b , and the second line is a string. I want to use formatted input to do file >> a >> b , and then unformatted input to get the characters of the string one by one. In between the two steps, I need to skip over the '\n' character at the end of the first line. I used while(file.get()<=' ' && !file.eof()); // skip all unprintable chars if(!file.eof()) file.unget(); // keep the eof sign once triggered to make the input format

Internal buffer used by standard input stream (pubsetbuf)

…衆ロ難τιáo~ 提交于 2019-12-20 06:38:34
问题 I'm trying to set the internal buffer of an input stream, but my implementation in C++17 doesn't implement pubsetbuf() for istringstream. I've tried some other techniques, but they are slow or copy the original buffer. I'm looking for a fast method that doesn't do any copying. It's closely related to this question about an output stream: Setting the internal buffer used by a standard stream (pubsetbuf) I've followed it closely, but the buffer for the input stream remains uninitialised/empty.

Getting an IStream from an OleVariant

时光毁灭记忆、已成空白 提交于 2019-12-20 02:10:50
问题 I am using Delphi along with WinHTTP to do an HTTP request to download some files from the internet, and I can do the request but I don't know how to get the IStream from the OleVariant that is returned from ResponseStream . I have spent a lot of time googling but I can't figure out how to do it. Here is what I have tried: var req: IWinHTTPRequest; instream: IStream; begin req := CoWinHTTPRequest.Create; req.Open('GET', 'http://google.com', false); req.Send(''); if req.Status <> 200 then

Read from cin or a file

為{幸葍}努か 提交于 2019-12-19 18:55:30
问题 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? 回答1: 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)

non-blocking std::getline, exit if no input

岁酱吖の 提交于 2019-12-19 05:31:19
问题 Currently I have a program that reads from the standard input, occasionally the program needs to just keep running if no input is made, usually this is a test script there is no 'enter' so to speak. program -v1 -v2 -v3 <input >output v1 - v3 are command line arguments respectively Basically the program spits out the command line arguments and their respective meaning to the program if no 'input' is given and then should exit. However at the moment if give it an empty test file or just run

How to use istream with strings

核能气质少年 提交于 2019-12-18 21:16:11
问题 I would like to read an file into a string. I am looking for different ways for how to do it efficiently. Using a fixed size *char buffer I have received an answer from Tony what creates a 16 kb buffer and reads into that buffer and appends the buffer till there is nothing more to read. I understand how it works and I found it very fast. What I don't understand is that in the comments of that answer it is said that this way copies everything twice. But as I understand it, it only happens in

istream and cin.get()

﹥>﹥吖頭↗ 提交于 2019-12-18 18:43:01
问题 I have a question about the difference between these two pieces of code: char buffer5[5]; cin.get(buffer5, 5); cout << buffer5; cin.get(buffer5, 5); cout << buffer5; and char buffer4; while (cin.get(buffer4)) { cout << buffer4; } In the first piece of code, the code gets 5 characters and puts it in buffer5. However, because you press enter, a newline character isn't put into the stream when calling get(), so the program will terminate and will not ask you for another round of 5 characters. In

istream and cin.get()

淺唱寂寞╮ 提交于 2019-12-18 18:41:15
问题 I have a question about the difference between these two pieces of code: char buffer5[5]; cin.get(buffer5, 5); cout << buffer5; cin.get(buffer5, 5); cout << buffer5; and char buffer4; while (cin.get(buffer4)) { cout << buffer4; } In the first piece of code, the code gets 5 characters and puts it in buffer5. However, because you press enter, a newline character isn't put into the stream when calling get(), so the program will terminate and will not ask you for another round of 5 characters. In

Discrepancy between istream's operator>> (double& val) between libc++ and libstdc++

对着背影说爱祢 提交于 2019-12-17 22:01:08
问题 With my recent upgrade to Mac OS X 10.9 the default standard C++ library changed from libstdc++ to libc++. Since then I observe unexpected behaviour of the stringstream operator>>(double) documented in the code example below. In summary the libc++ seems to have problems with extracting double values from stringstreams when the double value is followed by a letter. I already checked the standard (2003) but I can't find any specific information if extraction should work in this case or not. So

Using a regex_iterator on an istream

余生长醉 提交于 2019-12-17 20:53:59
问题 I want to be able to solve problems like this: Getting std :: ifstream to handle LF, CR, and CRLF? where an istream needs to be tokenized by a complex delimiter; such that the only way to tokenize the istream is to: Read it in the istream a character at a time Collect the characters When a delimiter is hit return the collection as a token Regexes are very good at tokenizing strings with complex delimiters: string foo{ "A\nB\rC\n\r" }; vector<string> bar; // This puts {"A", "B", "C"} into bar