iostream

Using boost::asio::async_read with stdin?

二次信任 提交于 2019-11-27 05:54:08
问题 short question: I have a realtime-simulation which is running as a backround process and is connected with pipes to the calling pogramm. I want to send commands to that process using stdin to get certain information from it via stdout. Now because it is a real-time process, it has to be a non blocking input. Is boost::asio::async_read in conjunction with iostream::cin a good idea for this task? how would I use that function if it is feasible? Any more suggestions? 回答1: Look at boost::asio:

Can you explain the concept of streams?

风流意气都作罢 提交于 2019-11-27 05:46:11
I understand that a stream is a representation of a sequence of bytes. Each stream provides means for reading and writing bytes to its given backing store. But what is the point of the stream? Why isn't the backing store itself what we interact with? For whatever reason this concept just isn't clicking for me. I've read a bunch of articles, but I think I need an analogy or something. The word "stream" has been chosen because it represents (in real life) a very similar meaning to what we want to convey when we use it. Let's forget about the backing store for a little, and start thinking about

checking data availability before calling std::getline

情到浓时终转凉″ 提交于 2019-11-27 05:23:46
I would like to read some data from a stream I have using std::getline . Below a sample using the std::cin . std::string line; std::getline( std::cin, line ); This is a blocking function i.e. if there is no data or line to read it blocks execution. Do you know if exists a function for checking data availability before calling std::getline ? I don't want to block. How can I check whether the stream buffer is full of data valid for a successful call to std::getline ? Whatever looks like the code below if( dataAvailableInStream() ) { std::string line; std::getline( std::cin, line ); } The

C++ Streams vs. C-style IO?

泪湿孤枕 提交于 2019-11-27 05:21:54
问题 I was coding some C++ for a small hobby project when I noticed that I'm using C-style operations to access IO ( printf , fopen , etc.). Is it considered "bad practice" to involve C functions in C++ projects? What are the advantages of using streams over C-style IO access? 回答1: This is an heated topic. Some people prefer to use the C++ IO since they are type-safe (you can't have divergence between the type of the object and the type specified in the format string), and flow more naturally with

ostream chaining, output order

我怕爱的太早我们不能终老 提交于 2019-11-27 05:07:00
I have a function that takes an ostream reference as an argument, writes some data to the stream, and then returns a reference to that same stream, like so: #include <iostream> std::ostream& print( std::ostream& os ) { os << " How are you?" << std::endl; return os; } int main() { std::cout << "Hello, world!" << print( std::cout ) << std::endl; } The output of this code is: How are you? Hello, world!0x601288 However, if I separate the chaining expressions into two statements, like this int main() { std::cout << "Hello, world!"; std::cout << print( std::cout ) << std::endl; } then I at least get

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

做~自己de王妃 提交于 2019-11-27 04:58:37
问题 I tried researching the difference between cout , cerr and clog on the internet but couldn't find a perfect answer. I still am not clear on when to use which. Can anyone explain to me, through simple programs and illustrate a perfect situation on when to use which one? I visited this site which shows a small program on cerr and clog , but the output obtained over there can also be obtained using cout . So, I'm confused over each one's exact use. 回答1: stdout and stderr are different streams,

<iostream> vs. <iostream.h> vs. “iostream.h”

故事扮演 提交于 2019-11-27 04:57:38
When including a header file in C++, what's the difference between... 1) including the .h versus not including the .h when wrapping it in < > signs? #include <iostream> vs. #include <iostream.h> 2) wrapping the header name in double quotes versus wrapping it in < > signs? #include <iostream.h> vs. #include "iostream.h" Thanks in advance! Adam Davis In short: iostream.h is deprecated - it is the original Stroustrup version, and iostream is the version from the standards committee. Generally compilers point them both to the same thing, but some older compilers won't have the older one. In some

Can I stop std::cout flushing on “\n”?

纵然是瞬间 提交于 2019-11-27 04:43:45
问题 According to to this post std::cout will automatically flush on \n when it is attached to an interactive device (e.g. a terminal window). Otherwise (e.g. when being piped to a file) it will act fully buffered and will only flush on .flush() or std::endl . Is there a way to override this behaviour in Microsoft Visual C++ so that I can select whether I want fully buffered or line buffered mode? 回答1: Contrary to anon's (Apr 28 '09) answer, this behavior has nothing to do with the operating

What the point of using std::ios_base::binary?

懵懂的女人 提交于 2019-11-27 04:42:05
问题 I had a issue with Linux file reading under Window. Here is the issue discussion: Using fstream::seekg under windows on a file created under Unix. The issue was workarounded by opening the text file with std::ios_base::binary specified. But what's the actual point with this mode? If specified, you can still work with your file as a text file (writting with mystream << "Hello World" << std::endl and reading with std::getline ). Under Windows, the only difference, I could notice is that

Using boost::iostreams::tee_device?

Deadly 提交于 2019-11-27 04:32:24
问题 Can someone help me? I am trying to do something like the following: #include <boost/iostreams/tee.hpp> #include <boost/iostreams/stream.hpp> #include <sstream> #include <cassert> namespace io = boost::iostreams; typedef io::stream<io::tee_device<std::stringstream, std::stringstream> > Tee; std::stringstream ss1, ss2; Tee my_split(ss1, ss2); // redirects to both streams my_split << "Testing"; assert(ss1.str() == "Testing" && ss1.str() == ss2.str()); But it won't compile in VC9: c:\lib\boost