Discussion:
Help a Noob
(too old to reply)
Jeff Taylor
2008-03-23 15:46:33 UTC
Permalink
Hi-

I'm an old time C programmer who is only recently trying to figure out C++.

I'm using C++ Builder to create a front end for a command line C program. Throughout the C program, it writes to stdout using printf statements. My front end has a Memo box that I would like to direct this output to but I don't know how to go about it. Could someone show me how to create a stream that I can use "cout" type syntax with (i.e. cout << "Print this" + "and that.") and then somehow connect the stream to the Memo box? OR maybe there is some other better way?

Thanks for the help!
Clayton Arends
2008-03-23 16:12:33 UTC
Permalink
Post by Jeff Taylor
I'm an old time C programmer who is only recently trying to figure out C++.
Welcome. C++ allows you to program like you're still using C. However, the
real benefit of programming in C++ is achieved by changing your design
approaches to that of Object-Oriented Design. There are plenty of free
on-line primers on the topic if you do an internet search.
Post by Jeff Taylor
I'm using C++ Builder to create a front end for a command line C program.
Throughout the C program, it writes to stdout using printf statements. My
front
end has a Memo box that I would like to direct this output to but I don't
know
how to go about it.
We love to help out here as much as possible. However, one of the best ways
of helping is to give people the tools to help themselves. If you perform a
newsgroup search (use http://groups.google.com) for keywords like "borland
redirect stdout" you will find hundreds of examples on how to capture the
standard output of a command-line application.

Once you've performed some ground-work, and if you run into problems
implementing the solutions you find, then post a new thread which details
what you've done and what isn't working.
Post by Jeff Taylor
Thanks for the help!
Good luck,
Clayton
Jeff Taylor
2008-03-24 14:54:00 UTC
Permalink
Thanks for the quick response.

I did some searching and came up with two possible solutions:

1) Use the AnsiString.printf function to print to a string and then
Memo1->Lines->Add(string)

This works, but a lot of my printf statements are multi-line outputs, i.e.
printf("This is line #1 \n and this is line #2"\n). Is it possible
possible to put a newline character in an AnsiString? (Using "\n" doesn't
work.) Or are AnsiStrings restricted to a single line?

2) sprintf to a char buffer and then do Memo1->Lines->Text = Memo1->
Lines->Text + buffer This works even with multi-line output, but doesn't
automatically scroll the TMemo window down so you can see the most recent
output.

It still seems like there should be a way to use an ostream similar to cout
and then Memo1->Lines->LoadFromStream(ostream), but I did not find any
examples of a method like this.
Thomas Maeder [TeamB]
2008-03-23 18:29:53 UTC
Permalink
[Please wrap lines in your future posts. Thanks!]
Post by Jeff Taylor
I'm using C++ Builder to create a front end for a command line C
program. Throughout the C program, it writes to stdout using printf
statements. My front end has a Memo box that I would like to direct
this output to but I don't know how to go about it. Could someone
show me how to create a stream that I can use "cout" type syntax
with (i.e. cout << "Print this" + "and that.") and then somehow
connect the stream to the Memo box? OR maybe there is some other
better way?
You could use freopen() to direct stdout to a file, and present that
file's contents in the Memo box.
Jeff Taylor
2008-03-24 14:36:36 UTC
Permalink
Post by Thomas Maeder [TeamB]
You could use freopen() to direct stdout to a file, and present that
file's contents in the Memo box.
I was hoping to avoid using a disk file. It seems that there should be a
way to do this with a TMemoryStream or something similar, but I haven't
figured it out yet.
Remy Lebeau (TeamB)
2008-03-24 16:39:03 UTC
Permalink
Post by Jeff Taylor
Throughout the C program, it writes to stdout using printf statements.
My front end has a Memo box that I would like to direct this output
to but I don't know how to go about it. Could someone show me
how to create a stream that I can use "cout" type syntax with (i.e.
cout << "Print this" + "and that.") and then somehow connect the
stream to the Memo box?
This has been asked and answered in detail many many times before. Please
go to http://www.deja.com and search through the newsgroup archives. In
short, you need to use CreateProcess() to spawn a new process for the C
program. CreateProcess() allows you to specify your own STDIN/OUT/ERR
handles for the new process. You can use CreatePipe() for that, and then
use ReadFile() and WriteFile() to read from, and write to, the pipes from
the C++ program.


Gambit
DreamChaser
2008-04-21 07:51:01 UTC
Permalink
Post by Jeff Taylor
Hi-
I'm an old time C programmer who is only recently trying to figure out C++.
I'm using C++ Builder to create a front end for a command line C program. Throughout the C program, it writes to stdout using printf statements. My front end has a Memo box that I would like to direct this output to but I don't know how to go about it. Could someone show me how to create a stream that I can use "cout" type syntax with (i.e. cout << "Print this" + "and that.") and then somehow connect the stream to the Memo box? OR maybe there is some other better way?
Thanks for the help!
Check out: http://www.leunen.com/cbuilder/redirect.html

Loading...