iostream

Strange output, not as expected

[亡魂溺海] 提交于 2019-12-01 01:43:30
sorry for asking you a stupid question, but I just can't figure out why I keep on getting this output. So here is my code: #include <cstdio> #include <iostream> using namespace std; unsigned n = 4242; int getRemainderOf(int m, int n, int& quotient); static int l = 0; int main() { int quotient; // the value of which should be changed after calling the func. for(int i=-1; i<=1; ++i) { for(int j=-1; j<=1; ++j) { if( i && j ) { cout << "("<< i*3 << "," << j*7 << ") " <<( getRemainderOf(i*3, 7*j, quotient) ) << " " << quotient <<endl; cout << "("<< i*7 << "," << j*3 << ") " << getRemainderOf(i*7, 3

How to read space separated numbers from console?

房东的猫 提交于 2019-12-01 00:43:39
问题 I'm trying to do a simple task of reading space separated numbers from console into a vector<int> , but I'm not getting how to do this properly. This is what I have done till now: int n = 0; vector<int> steps; while(cin>>n) { steps.push_back(n); } However, this requires the user to press an invalid character (such as a ) to break the while loop. I don't want it. As soon as user enters numbers like 0 2 3 4 5 and presses Enter I want the loop to be broken. I tried using istream_iterator and cin

A line-based thread-safe std::cerr for C++

风流意气都作罢 提交于 2019-11-30 23:53:26
What is the easiest way to create my own std::cerr so that it is line-by-line thread-safe. I am preferably looking for the code to do it. What I need is so that a line of output (terminated with std::endl ) generated by one thread stays as a line of output when I actually see it on my console (and is not mixed with some other thread's output). Solution : std::cerr is much slower than cstdio. I prefer using fprintf(stderr, "The message") inside of a CriticalSectionLocker class whose constructor acquires a thread-safe lock and the destructor releases it. If available, osyncstream (C++20) solves

In simple terms, what is the purpose of flush() in ostream

亡梦爱人 提交于 2019-11-30 22:19:36
By definition taken from: http://www.cplusplus.com/reference/iostream/ostream/flush/ , it is not clear why the function exists, and for what purpose you would use it for. Why not call flush(), every time your write to the stream? Mysticial In all likelihood, the word flush comes from exactly what you'd flush in real-life. A toilet... So let's try a toilet analogy: Flushing every time a new one drops into the bowl is very time-consuming and a complete waste of water. That's a big problem today where everyone's trying to be environmentally friendly. So what do you do instead? You buffer it by

Why are iostreams not copyable?

十年热恋 提交于 2019-11-30 22:15:16
It's possible to make a local copy of an iostream object, using rdbuf and copyfmt . This allows formatting changes to be locally scoped: std::ostream & operator << ( std::ostream & os, foo const & smth ) { cloned_ostream cs( os ); cs << std::hex << smth.num; // os is not switched to hexadecimal, which would be a confusing side-effect return os; } Why don't the stream classes provide copy constructors to do this? Have relevant C++ best practices changed since they were designed as non-copyable? Copying and moving are value-semantic operations. To define them, you first have to decide what

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

Possible to stop cin from waiting input?

五迷三道 提交于 2019-11-30 19:03:34
In a graphical application I execute debug commands using the console input. When the console is created a new thread is also created to gather the user commands that handles all that input, the graphical application continues running parallel. I use boost::thread library. It works good so far, however I haven't found a nice solution to stop the execution of this thread. The thread is always waiting for a user input: while(appRunning) { std::cin>>theUserCommand; // ...do stuff } Then when the graphical application ends, it will stop all console functions, in which I include the thread:

A line-based thread-safe std::cerr for C++

廉价感情. 提交于 2019-11-30 18:05:55
问题 What is the easiest way to create my own std::cerr so that it is line-by-line thread-safe. I am preferably looking for the code to do it. What I need is so that a line of output (terminated with std::endl ) generated by one thread stays as a line of output when I actually see it on my console (and is not mixed with some other thread's output). Solution : std::cerr is much slower than cstdio. I prefer using fprintf(stderr, "The message") inside of a CriticalSectionLocker class whose

When to use printf/scanf vs cout/cin?

你离开我真会死。 提交于 2019-11-30 14:12:14
问题 I'm testing some snippets I found off the web using g++ from MinGW. This is the C++ compiler...why then does it correctly compile C....why do people intertwine C and C++. The concrete question is: Is it O.K. to use both C and C++ and compile under g++. If the answer is yes, this makes my life easy as I do not have to modify the code. Oddly enough...to get some C++ to work, particularly when passing a string to an ifstream constructor it requires a C type string... My guess would be that

iostream and large file support

狂风中的少年 提交于 2019-11-30 14:04:34
I'm trying to find a definitive answer and can't, so I'm hoping someone might know. I'm developing a C++ app using GCC 4.x on Linux (32-bit OS). This app needs to be able to read files > 2GB in size. I would really like to use iostream stuff vs. FILE pointers, but I can't find if the large file #defines (_LARGEFILE_SOURCE, _LARGEFILE64_SOURCE, _FILE_OFFSET_BITS=64) have any effect on the iostream headers. I'm compiling on a 32-bit system. Any pointers would be helpful. vladr This has already been decided for you when libstdc++ was compiled, and normally depends on whether or not _GLIBCXX_USE