In my project (Unreal Engine 4) I don't have an output stream - instead of this I can communicate via UE_LOG function, which works pretty much similar to printf(). The problem is that I just made a .dll library (without Unreal includes) which I want to communicate through the iostream. My idea is - inside .dll library I use standard cout to write messages into ostream, I use all of it in Unreal Engine functions, where I grab ostream in form of string and output it into UE_LOG function.
Problem is I always treated std::cout as a part of magic, without thinking what is really inside (I am pretty sure most of us did). How I can handle this? Easy ways won't work (like grabbing stringstream and outputing it into UE_LOG).
My idea is - inside .dll library I use standard cout to write messages into ostream
You actually can replace the output buffer used with std::cout with your own implementation. Use the std::ostream::rdbuf() function to do so (example from the reference docs):
#include <iostream>
#include <sstream>
int main()
{
std::ostringstream local;
auto cout_buff = std::cout.rdbuf(); // save pointer to std::cout buffer
std::cout.rdbuf(local.rdbuf()); // substitute internal std::cout buffer with
// buffer of 'local' object
// now std::cout work with 'local' buffer
// you don't see this message
std::cout << "some message";
// go back to old buffer
std::cout.rdbuf(cout_buff);
// you will see this message
std::cout << "back to default buffer\n";
// print 'local' content
std::cout << "local content: " << local.str() << "\n";
}
(in case my edit won't be positively reviewed)
From OP: Thanks to your hints I finally found how to solve my problem. Suppose I want to get stream from cout and send it to printf (because I think stdio library is superior to iostream). Here how I can do this:
#include <iostream>
#include <sstream>
#include <cstdio>
using namespace std;
class ssbuf : public stringbuf{
protected:
int sync(){
printf("My buffer: %s",this->str().c_str());
str("");
return this->stringbuf::sync();
}
};
int main(){
ssbuf *buf = new ssbuf();
cout.rdbuf(buf);
cout<<"This is out stream "<<"and you cant do anything about it"<<endl;
cout<<"(don't) "<<"Vote Trump"<<endl;
}
Code is very raw, but it does it's job. I made child class of buffer which has method sync() downcasting original virtual method sync(). Except this it works like usual buffer, just grabs all console-out stream - exactly what we wanted. The str("") inside is to clean the buffer - probably not outputted stream doesn't clean itself.
Great thanks for help! Big GRIN for you! :D
Related
What I am trying to achieve is to use the standard std::cout and if you understand, reverse the output to an input so another part of my program can read it. No I cannot just call a function in the other part of my program because every function is and must stay private so this seems the only way to do it. I did a similar thing in java by redirecting the default output stream to my own custom one but I am somewhat new to c++. This is what I did in java:
System.setOut(customPrintStream);
Does anyone know of an alternative for c++ or a way to get whatever is printed to console?
The c++ standard library supports the concept of 'reverse iterators'.
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
int main()
{
auto s = std::string("Hello, World");
std::copy(std::rbegin(s), std::rend(s),
std::ostream_iterator<char>(std::cout));
std::cout << std::endl;
return 0;
}
expected output:
dlroW ,olleH
The streams in C++ read from and write to std::streambuf objects. You can replace the stream buffer of an std::ostream object using the rdbuf() function. For example, to capture all output written to std::cout in a std::string you could use something like this:
std::ostreamstream stream;
std::streambuf* sbuf = std::cout.rdbuf(stream.rdbuf());
// use code whose output is written to `std::cout`
std::cout.rdbuf(sbuf); // restore the original stream buffer
std::string output = stream.str();
In a real implementation you'd probably use an RAII approach to replace/restore the stream buffer. Also, you might want to use a custom stream buffer rather than a std::stringbuf but the basic remain as is.
I'm adapting a console based program to GUI.
The console program reads a text file and "compiles" it.
My GUI application reads the text file and displays in a RichTextBox.
I'm looking for a method to treat the RichTextBox as a C++ std::istream. This would allow me to use code from the console program without modifying it.
I searched the web and StackOverflow and didn't find any solutions for treating a RichTextBox as an std::istream.
Does anybody know of any Winforms library functions that would allow treating of RichTextBox as an std::istream?
My ideas:
Create an adapter to treat RichTextBox as a stream.
Change the console program to pass a "getline" function to the
compiler portion, and have two getline functions (one as the
std::getline, another to get a line from the RichTextBox).
Write the RichTextBox contents to a file and feed the file to the
compiler.
I'm using Visual Studio 2010 on Win 7 using ".NET" 4.0, using C++ (don't suggest any C# techniques as I'm not fluent in translating).
In real C++, you can create a stream buffer from an RTF control like this:
class RTF_buf : public std::streambuf {
std::vector<char> buffer;
public:
RTF_buf(HWND ctrl) {
DWORD len = SendMessage(ctrl, WM_GETTEXTLENGTH, 0, 0);
buffer.resize(len+1);
SendMessageA(ctrl, WM_GETTEXT, len+1, (LPARAM)&buffer[0]);
setg(&buffer[0], &buffer[0], &buffer[len]);
}
};
Note that this isn't actually restricted to an RTF control. Just for one other example, it'll also work fine with a normal EDIT control.
C++/CLI adds a few wrinkles to this. First of all, you're dealing with "wide" characters in the RichTextBox. Second, you won't (normally) start with an HWND -- you have to retrieve that from the System.Windows.Forms.RichTextBox via its Handle property. This, unfortunately, returns the HWND as an IntPtr instead of an HWND, so you have to add a cast to get it to the right type. That makes the code a little uglier, but nothing too terrible:
#include <windows.h>
#include <streambuf>
#include <iostream>
#include <vector>
#include <algorithm>
#pragma comment(lib, "user32.lib")
using namespace System;
using namespace System::Windows::Forms;
class RTF_buf : public std::wstreambuf {
std::vector<wchar_t> buffer;
public:
RTF_buf(RichTextBox^ control) {
HWND ctrl = *reinterpret_cast<HWND *>(&control->Handle);
int len = SendMessage(ctrl, WM_GETTEXTLENGTH, 0, 0);
buffer.resize(len+1);
SendMessage(ctrl, WM_GETTEXT, len+1, (LPARAM)&buffer[0]);
setg(&buffer[0], &buffer[0], &buffer[len]);
}
};
We can create a buffer and istream something like this:
RTF_buf b(this->richTextBox1);
std::wistream in(&b);
Finally, we can read data from our stream and process them like we would essentially any other (wide) stream. For example:
wchar_t ch;
while (in >> ch)
// do something with ch
So C++/CLI does add a little complexity to the task, but ultimately only a little--mostly the one line to obtain the handle of the control, and cast it to the correct type. Other than that the code for the buffer class barely needs to change at all, and instantiating and using it changes only to the extent that we're working with wide characters instead of narrow.
The proper way to create a stream plugging into the IOStreams library is to implement a stream buffer, i.e., to derive from std::streambuf or std::wstreambuf (I'm not a Windows programmer but my understanding is that most code travels in terms of wchar_t rather than char) and override the suitable virtual member functions. Assuming you can get characters in bunches (possible all in a bukl) all you'd really overload is underflow() which is called if the input buffer was exhausted. If you can get all the characters during construction you can also set up a buffer.
Once you have a stream buffer you can use a pointer to the stream buffer to initialize an std::istream. Here is a simple example which uses a memory arean passed in the constructor as its input:
#include <iostream>
#include <streambuf>
class membuf
: std::streambuf {
public:
membuf(char* buffer, std::size_t size) {
this->setg(buffer, buffer, buffer + size);
}
};
int main() {
char input[] = "hello, world!\n";
membuf sbuf(input, sizeof(input - 1));
std::istream in(&sbuf);
char buffer[100];
if (in.getline(buffer, sizeof(buffer)) {
std::cout << "read '" << buffer << "'\n";
}
else {
std::cout << "ERROR: failed to read a line but Dietmar said...!?!\n";
}
}
I'm adding C++ code to an iOS application, and I would like to use a UITextView as a way to display what's going through std::cout. I don't want to modify the C++ code too much.
So far, I have defined a string stream named stdcout, in the scope of the C++ code I'm interested in capturing the output, and I'm updating the UITextView after the C++ block returns. This is a bit intrusive, as I need to do some manual text replacing, and it's error prone.
Is there a better way to do this ?
You can look at rdbuf().
If you care about performance/flexibility, you could write a custom stream buffer and implement the overflow members so that you get "automatic" "live" updating.
Here's a simple example relaying to a stringstream:
#include <sstream>
#include <iostream>
int main()
{
std::ostringstream oss;
auto saved = std::cout.rdbuf(oss.rdbuf());
std::cout << "hello world" << std::endl;
std::cout.rdbuf(saved);
return oss.str().length();
}
This program exits with exitcode '12' on my cygwin shell:
./test.exe; echo $?
12
I have a c++ program that prints to the screen using std::cout.
Sometimes I need to run it as a service. Is there any way of seeing the cout outputs when it's running as a Windows service?
Redirecting the output to a file or some sort of debugging program would be ideal.
Obviously I could replace the cout with a function that writes to a file, and that's probably what I'll do, but I'm curious to know if there are other solutions.
There's basically infinite options. The first few that come to mind:
Pass around an ostream reference
You could pass around an std::ostream reference:
void someFunc(std::ostream& out) {
//someFunc doesn't need to know whether out is a file, cout, or whatever
out << "hello world" << std::endl;
}
Replace cout underlying buffer with a file
Example from cplusplus.com:
streambuf *psbuf, *backup;
ofstream filestr;
filestr.open ("test.txt");
backup = cout.rdbuf(); // back up cout's streambuf
psbuf = filestr.rdbuf(); // get file's streambuf
cout.rdbuf(psbuf); // assign streambuf to cout
cout << "This is written to the file";
There's a 1-liner with freopen, but I have a creeping feeling (and this seems to reenforce it in the comments) that it's undefined behavior since stdin and cout can be un-synchronized.
freopen("/path/to/file", "r", stdout);
//cout is now writing to path/to/file
A logging library
Not sure of a good one of the top of my head, but you could go full out and use some type of logging library. (There's also Windows events, though depending on what you're outputting, that might not make sense.)
Piping
I doubt this is possible with a Windows service, but if it is, there's always the classic redirection:
blah.exe > C:\path\file
The easy solution is SetStdHandle(STD_OUTPUT_HANDLE, your_new_handle).
You could do something like this:
class MyTerminal {
std::stringstream terminalText;
}
class MyWindow {
public:
void OnUpdate();
protected:
CTextbox m_textbox;
MyTerminal m_terminal;
}
void MyWindow::OnUpdate()
{
m_textBox.setText(m_terminal.terminalText.str());
m_terminal.terminalText.str(std::string());
}
Can a windows message box be display using the cout syntax?
I also need the command prompt window to be suppressed / hidden.
There are ways to call the messagebox function and display text through its usage, but the main constraint here is that cout syntax must be used.
cout << "message";
I was thinking of invoking the VB msgbox command in the cout output, but couldn't find anything that worked.
Any ideas?
C++ streams work with console or file streams. Windows work on a more or less completely different paradigm, so the cout context isn't really a good one for working with this. You could probably completely mash up something that would end up more or less working, and looking more or less similar to this syntax, but it's not really worth it when you can just do:
MessageBox( NULL, message, "", MB_OK );
See the full docs on MessageBox for more info.
First thing you should take into account is that MessageBox stops the thread until you close the window. If that is the behavior you desire, go ahead.
You can create a custom streambuf and set it to std::cout:
#include <windows.h>
#include <sstream>
#include <iostream>
namespace {
class mb_streambuf : public std::stringbuf {
virtual ~mb_streambuf() { if (str().size() > 0) sync(); }
virtual int sync() {
MessageBoxA(0, str().c_str(), "", MB_OK);
str("");
return 0;
}
} mb_buf;
struct static_initializer {
static_initializer() {
std::cout.rdbuf(&mb_buf);
}
} cout_buffer_switch;
}
int main()
{
std::cout << "Hello \nworld!"; // Will show a popup
}
A popup will be shown whenever std::cout stream is flushed.
By including sstream, you can use std::ostringstream and build a message using the iostream library. You can then call .str().c_str() and get a char * to pass to MessageBox.
When confronted with this in the past, I've used a stringstream along with a manipulator that displays the current contents of the stringstream using MessageBox:
#include <windows.h>
#include <sstream>
#include <ostream>
std::ostream &MessageBox(std::ostream &s) {
std::ostringstream *st = dynamic_cast<std::ostringstream *>(&s);
if (NULL != st)
::MessageBox(NULL, st->str().c_str(), "", MB_OK);
return s;
}
To use this, the syntax looks a fair amount like using cout, but with MessageBox replacing std::endl. For example:
std::ostringstream stm;
stm << " blah blah blah. Value: " << 1213.1231 << MessageBox;
Edit: mostly for fnieto. In this case, the downcast really is necessary. The reason is fairly simple: a typical inserter receives and returns a reference to an ostream:
std::ostream &operator<<(std::ostream &os, T const &t) {
// code here to insert t into os, then return os;
}
This takes the original stringstream object and silently (and safely) casts it up to a simple ostream. That's fine in itself, and works fine for most inserters and manipulators, because they only interact with the ostream interface themselves.
This manipulator, however, is a bit different -- it uses the str() member, which ostream doesn't define at all. For our call to str() to resolve and compile, we have to convert the ostream & to an ostringstream &, so the compiler is aware that the object we're working with really will have a str() member.
To eliminate the downcast, we'd really only have one choice: make its parameter an ostringstream &. That would work as long as we never chained operators:
my_stream << x;
my_stream << MessageBox;
but trying to chain those would fail:
// should be equivalent:
my_stream << x << MessageBox;
Worse, the compiler's error message will probably try to tell the user something about std::basic_ostream<char>::str(), which isn't mentioned in the user's code at all. Worse still, most people are sufficiently accustomed to chaining or not giving identical results that it would probably take them a while to even figure out why the code sometimes worked fine, and other times failed to compile, with a completely indecipherable error message.
No simple way anyway.
The c in cout stands for console, so you're probably out of luck.
If it's just the syntax you're looking to copy, then you could write your own stream class that creates a message box under the hood and displays it.
You may want to check this out:
How can I redirect stdout to some visible display in a Windows Application?
Can a windows message box be display using the cout syntax?
You can't do it with std::cout. std::cout doesn't even promise to handle Unicode/wide characters (see std::wcout), although Windows's cout has no trouble with wide characters.
You could easily do it with the same syntax; that is, you could easily write a library that overloads operator<< to display dialog boxes. Trying to pass all the information to the dialog box that way would be very difficult, though (how would you you say which buttons to show, what those buttons should do when pressed, where those buttons should be, and the size and position of the window itself?).
You may want to look at something like ncurses. The syntax is different, but I have a
feeling it's what your coworker is looking for.