iostream

Unexpected exception in std::ifstream

情到浓时终转凉″ 提交于 2019-12-17 21:03:49
问题 Experimenting with I/O I get an exception where no exception should have been thrown: #include <iostream> #include <fstream> int main() { std::ifstream f("/tmp"); std::cout << "Exception Flags: " << f.exceptions() << std::endl; if(f >> std::ws) std::cout << "This will not succeed" << std::endl; else std::cout << "Ok - it fails" << std::endl; return 0; } But the output is: Exception Flags: 0 terminate called after throwing an instance of 'std::ios_base::failure' what(): basic_filebuf:

Effective use of C++ iomanip library

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 19:23:07
问题 I created a Vector class in C++ and it works great for my problems. I am now cleaning it up, and I ran into the following piece of code: std::ostream& operator<<(std::ostream &output, const Vector &v){ output<<"[" <<std::setiosflags(std::ios::right | std::ios::scientific) <<std::setw(23) <<std::setprecision(16) <<v._x<<", " <<std::setiosflags(std::ios::right | std::ios::scientific) <<std::setw(23) <<std::setprecision(16) <<v._y<<", " <<std::setiosflags(std::ios::right | std::ios::scientific)

C++ iostream Corruption using stringstream

浪尽此生 提交于 2019-12-17 17:10:28
问题 I'm trying to write a really simple thread-safe logger. Ideally, I wanted it to work just like std::cout , wherein you could just overload the << operator and have everything magically show up in the log. I'm on a Windows machine, so here's the approach I tried: // Threadsafe logger class Logger { public: Logger() { InitializeCriticalSection(&s); } ~Logger() { DeleteCriticalSection(&s); } void Log(std::ostream const& os) { EnterCriticalSection(&s); //std::cout << static_cast<std::stringstream

Is it possible to use cin with Qt?

我的梦境 提交于 2019-12-17 16:46:08
问题 Is it possible to use cin in Qt? I can use cout but cannot find examples of how to use cin within a Qt console application. 回答1: I tested out Kaleb Pederson's answer, and found a more consise way than the solution he presented (though I have to thank him for pointing me to the right direction): QTextStream qtin(stdin); QString line = qtin.readLine(); // This is how you read the entire line QString word; qtin >> word; // This is how you read a word (separated by space) at a time. In other

Wrapping FILE* with custom std::ostream

ε祈祈猫儿з 提交于 2019-12-17 16:37:37
问题 I have a function which works with a std::ostream . I need to support using a C file handle ( FILE* ). Should I be creating my own subclass of std::ostream which delegates to a FILE* ? 回答1: As Ben Voigt points out, you want to subclass streambuf . There are pages on the University of Southern California's website which have the documentation, header, and source for a GNU implementation of a streambuf subclass ( stdiobuf ) that wraps a FILE* . It has some dependencies on the library it is a

Characters extracted by istream >> double

心已入冬 提交于 2019-12-17 16:37:35
问题 Sample code at Coliru: #include <iostream> #include <sstream> #include <string> int main() { double d; std::string s; std::istringstream iss("234cdefipxngh"); iss >> d; iss.clear(); iss >> s; std::cout << d << ", '" << s << "'\n"; } I'm reading off N3337 here (presumably that is the same as C++11). In [istream.formatted.arithmetic] we have (paraphrased): operator>>(double& val); As in the case of the inserters, these extractors depend on the locale’s num_get<> (22.4.2.1) object to perform

Is there a way to check if an istream was opened in binary mode?

人走茶凉 提交于 2019-12-17 16:32:29
问题 I'm using an istream which could be stringstream, ifstream or a user-defined stream type and I need to know if, in the case of an ifstream, it was not opened in binary mode (so I can throw an exception). I have tried the following method: if ((_is.flags() & ios::binary) == 0) throw exception(...) but no exception is ever thrown. The test fails in this case because _is.flags() returns 0x201 and ios::binary is 0x20. Is there a way to find out if a stream was opened in text mode? 回答1: flags()

Injecting string to 'cin'

隐身守侯 提交于 2019-12-17 16:28:23
问题 I have a function that reads user input from std::cin, and I want to write a unittest that inserts some strings into std::cin, such that later extraction from std::cin will read that string instead of pausing for keyboard input. Ideally, I would change the function signature so that I can pass a custom istream as parameters, but I can't do that here since I have a fixed interface that I cannot change. cin.putback() is almost what I wanted, however it is inserting only one character at a time,

In Java, how can I redirect System.out to null then back to stdout again?

北战南征 提交于 2019-12-17 15:46:47
问题 I've tried to temporarily redirect System.out to /dev/null using the following code but it doesn't work. System.out.println("this should go to stdout"); PrintStream original = System.out; System.setOut(new PrintStream(new FileOutputStream("/dev/null"))); System.out.println("this should go to /dev/null"); System.setOut(original); System.out.println("this should go to stdout"); // This is not getting printed!!! Anyone have any ideas? 回答1: Man, this is not so good, because Java is cross-platform

Who architected / designed C++'s IOStreams, and would it still be considered well-designed by today's standards? [closed]

我是研究僧i 提交于 2019-12-17 15:03:56
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . First off, it may seem that I'm asking for subjective opinions, but that's not what I'm after. I'd love to hear some well-grounded arguments on this topic. In the hope of getting some insight into how a modern streams / serialization framework ought to be designed, I recently