istream

How to use istream with strings

拥有回忆 提交于 2019-11-30 18:35:01
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 the memory, not from the disk, so it is almost unnoticable. Is it a problem that it copies from the

Reading binary istream byte by byte

北慕城南 提交于 2019-11-30 17:09:18
I was attempting to read a binary file byte by byte using an ifstream. I've used istream methods like get() before to read entire chunks of a binary file at once without a problem. But my current task lends itself to going byte by byte and relying on the buffering in the io-system to make it efficient. The problem is that I seemed to reach the end of the file several bytes sooner than I should. So I wrote the following test program: #include <iostream> #include <fstream> int main() { typedef unsigned char uint8; std::ifstream source("test.dat", std::ios_base::binary); while (source) { std::ios

Reading binary istream byte by byte

你。 提交于 2019-11-30 16:26:16
问题 I was attempting to read a binary file byte by byte using an ifstream. I've used istream methods like get() before to read entire chunks of a binary file at once without a problem. But my current task lends itself to going byte by byte and relying on the buffering in the io-system to make it efficient. The problem is that I seemed to reach the end of the file several bytes sooner than I should. So I wrote the following test program: #include <iostream> #include <fstream> int main() { typedef

How to store formatting settings with an IOStream?

最后都变了- 提交于 2019-11-30 03:23:20
问题 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

How to convert QByteArray to std::istream or std::ifstream?

橙三吉。 提交于 2019-11-29 15:28:11
I want to create istream from QByteArray at runtime, without saving a physical file in memory of QByteArray . I found that there are many ways to do the opposite conversion, i.e. istream to QByteArray , but not this one. How to accomplish that? To read via std::istringstream from QByteArray seems quite easy: testQByteArray-istream.cc : #include <iostream> #include <sstream> #include <QtCore> int main() { qDebug() << "Qt Version:" << QT_VERSION_STR; // make a QByteArray QByteArray data("Hello Qt World."); // convert to std::string std::istringstream in(data.toStdString()); // read from

Input from stream to enum type

浪尽此生 提交于 2019-11-29 13:34:17
How to input from stream to enum type? I can do it so unsigned int sex = 0; stream >> sex; student.m_bio.sex = static_cast<Sex>(sex); Otherwise? inline std::istream & operator>>(std::istream & str, Sex & v) { unsigned int sex = 0; if (str >> sex) v = static_cast<Sex>(sex); return str; } If you want to ensure that the value is valid, you can do something like this: enum Sex { Male, Female, Sex_COUNT }; inline std::istream & operator>>(std::istream & str, Sex & v) { unsigned int sex = 0; if (!(str >> sex)) return str; if (sex >= Sex_COUNT) { str.setstate(str.rdstate() | std::ios::failbit);

Using a regex_iterator on an istream

折月煮酒 提交于 2019-11-28 14:03:24
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 transform(sregex_iterator(foo.cbegin(), foo.cend(), regex("(.*)(?:\n\r?|\r)")), sregex_iterator(), back

What Effect Would LWG2349 Have?

家住魔仙堡 提交于 2019-11-28 13:35:12
While libstdc++ does not, libc++ does follow the standard which states that passing ios_base::failbit to basic_istream::exceptions has no effect on formatted input. For example this code: istringstream is{"ASD"}; double foo; is.exceptions(istream::failbit); try { is >> foo; cout << foo << endl; } catch(ios_base::failure& fail) { cout << "ouch\n"; } Would result in: "ouch" on libstdc++ "0" on libc++ My reading of LWG2349 is that it would cause basic_istream to not throw on any formatted input. For example LWG2349 proposes a change to the standard's 27.7.2.3 [istream]/1 which was cited with

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

你离开我真会死。 提交于 2019-11-28 12:11:40
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 I would be grateful for any input whether this is a bug in libc++ or libstdc++. #include <sstream>

FILE * and istream: connect the two?

戏子无情 提交于 2019-11-28 11:14:43
Suppose I "popen" an executable, I get a FILE* in return. Furthermore, suppose I'd like to "connect" this file to an istream object for easier processing, is there a way to do this? There is no standard way but if you want a quick solution you can get the file descriptor with fileno() and then use Josuttis' fdstream . There may be similar efforts around but I used this in the distant past and it worked fine. If nothing else it should be a very good map to implementing your own. ihuk You can get away by deriving std::basic_streambuf or std::streambuf classes. Something along these lines: