Converting c++ iostream and strstream code to c#

R

rh5680

Hi group,
I'm converting a large old C++ program to C#. It has lots and lots and
lots of iostream and strstream io. Is there an equivalent io
capability in C#? Any suggestions on the most efficient way to do
this. Below is a little bit of c++ code I found on the web that shows
what I'm trying to do.
Thanks /Mike

#include <iostream>
#include <strstream>
using namespace std;

int main()
{
char iostr[80];

strstream strio(iostr, sizeof(iostr), ios::in | ios::blush:ut);

int a, b;
char str[80];

strio << "10 20 testing ";
strio >> a >> b >> str;
cout << a << " " << b << " " << str << endl;

return 0;
}
 
P

Peter Duniho

rh5680 said:
Hi group,
I'm converting a large old C++ program to C#. It has lots and lots and
lots of iostream and strstream io. Is there an equivalent io
capability in C#? Any suggestions on the most efficient way to do
this.

Define "most efficient". Least memory? Fastest performance? Least
development time? Fewest bugs?

If you want to treat a string in C# as a stream, the closest you can
come is using the StringWriter and StringReader classes. Alternatively,
you can use the StreamWriter and StreamReader classes with a
MemoryStream, where the only time an actual string is involved is when
it's passed to the StreamWriter or returned from the StreamReader.

But, of course, stream-like access to a string isn't always all that
useful anyway.

The StringWriter and StringReader classes allow the use of a
string-based TextWriter and TextReader, for those specific scenarios
where you're passing the object to a class or method that uses that as
its output or input. But most of the time, if all you want is to format
a string, or print it out, directly manipulating the string is easier.

The bottom line here is that there's nothing in C# that provides the
same kind of syntax that C++ has with respect to i/o streams (thank
goodness...I nearly fainted the first time I ever saw that in C++, and
to this day I think it's a ridiculous overloading of the << and >>
operators).

Since you're going to have to do some semantic translation of the C++
code anyway, you might as well make a decision decision based not on
what the C++ code looks like, but rather what you think will be the most
useful and maintainable in the context of the C# code. No matter what
the choice you make, it won't be that hard to convert the C++ code. It
all comes down to formatting and appending strings anyway.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top