tags:

views:

223

answers:

7

Is there a C# library that provides the functionality of ">>" and "<<" for IO in C++? It was really convenient for console apps. Granted not a lot of console apps are in C#, but some of us use it for them.

I know about Console.Read[Line]|Write[Line] and Streams|FileStream|StreamReader|StreamWriter thats not part of the question.

I dont think im specific enough

int a,b;
cin >> a >> b;

IS AMAZING!!

string input = Console.ReadLine();
string[] data = input.split( ' ' );
a = Convert.ToInt32( data[0] );
b = Convert.ToInt32( data[1] );

... long winded enough? Plus there are other reasons why the C# solution is worse. I must get the entire line or make my own buffer for it. If the line im working on is IDK say the 1000 line of Bells Triangle, I waste so much time reading everything at one time.

EDIT: GAR!!!

OK THE PROBLEM!!!

Using IntX to do HUGE number like the .net 4.0 BigInteger to produce the bell triangle. If you know the bell triangle it gets freaking huge very very quickly. The whole point of this question is that I need to deal with each number individually. If you read an entire line, you could easily hit Gigs of data. This is kinda the same as digits of Pi. For Example 42pow1048576 is 1.6 MB! I don't have time nor memory to read all the numbers as one string then pick the one I want

+1  A: 

Nope. You're stuck with Console.WriteLine. You could create a wrapper that offered this functionality, though.

JSBangs
Well, Console.Out is a Stream ;)
Actually, you *couldn't* create such a wrapper... I just tried : when overriding the << and >> operators, the second parameter must be an int :S
Thomas Levesque
@wwosik : no it's not... it's a TextWriter
Thomas Levesque
@Thomas - yes, you're right and since it's programming better be precise ;) Though TextWriter and a console stream are very similar in meaning
@wwosik: `TextWriter` is strings. `Stream` is bytes. Big difference.
John Saunders
A: 

You can Use Console.WriteLine , Console.ReadLine ..For the purpose.Both are in System NameSpace.

Ananth
+1  A: 

You have System.IO.Stream(Reader|Writer) And for console: Console.Write, Console.Read

+4  A: 

No, and I wouldn't. C# != C++

You should try your best to stick with the language convention of whatever language you are working in.

Robert Greiner
To be fair, Console.WriteLine is nothing else than writing to the Console.Out stream.
Now see this is one place I think C# EPIC FAILS! C# IO makes it easy to read straight up binary values, kinda hard to read real text.
Buttink
@Buttink: I'm not sure what you mean by "hard to read real text". Console.Read/Write is textual, since `Console.Out` and `Console.In` are TextReader/Writers. Do you mean it's hard to read a textual int or word?
Simon Buchan
System.IO.File.ReadAllText("filename.txt") isn't that bad either '_
A: 

Not that I know of. If you are interested of the chaining outputs you can use System.Text.StringBuilder. http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(VS.71).aspx

StringBuilder builder = new StringBuilder();
builder.Append("hello").Append(" world!");
Console.WriteLine(builder.ToString());

Perhaps not as pretty as C++, but as another poster states, C# != C++.

Patrick
A: 

This is not even possible in C#, no matter how hard you try:

  1. The left hand side and right hand side of operators is always passed by value; this rules out the possibility of cin.
  2. The right hand side of << and >> must be an integer; this rules out cout.

The first point is to make sure operator overloading is a little less messy than in C++ (debatable, but it surely makes things a lot simpler), and the second point was specifically chosen to rule out C++'s cin and cout way of dealing with IO, IIRC.

Ruben
+1  A: 

I think I get what you are after: simple, default formatted input. I think the reason there is no TextReader.ReadXXX() is that this is parsing, and parsing is hard: for example: should ReadFloat():

  • ignore leading whitespace
  • require decimal point
  • require trailing whitespace (123abc)
  • handle exponentials (12.3a3 parses differently to 12.4e5?)

Not to mention what the heck does ReadString() do? From C++, you would expect "read to the next whitespace", but the name doesn't say that.

Now all of these have good sensible answers, and I agree C# (or rather, the BCL) should provide them, but I can certainly understand why they would choose to not provide fragile, nearly impossible to use correctly, functions right there on a central class.

EDIT: For the buffering problem, an ugly solution is:

static class TextReaderEx {
    static public string ReadWord(this TextReader reader) {
        int c;
        // Skip leading whitespace
        while (-1 != (c = reader.Peek()) && char.IsWhiteSpace((char)c)) reader.Read();
        // Read to next whitespace
        var result = new StringBuilder();
        while (-1 != (c = reader.Peek()) && !char.IsWhiteSpace((char)c)) {
            reader.Read();
            result.Append((char)c);
        }
        return result.ToString();
    }
}

...
    int.Parse(Console.In.ReadWord())
Simon Buchan
Well, Looks like I have to make it >.> Sigh... Thanks for the explanation as why they probably didn't.
Buttink
@Buttink: Take a look at http://www.complore.com/input-and-output-c-formatted-input - looks like a good starting point.
Simon Buchan