I'm kind of new to C# (come from a C++ background), and I've got a question regarding file reading. Is there a way to read a large number of ints or doubles from a file directly (as easy as in C++)?

I've looked into StreamReader, StringReader and BinaryReader and while I can get it done, I think I must be missing something since it's not nearly as straightforward as it is in C++. So far I've used StreamReader to read the entire file, then use ' ' as a delimiter and get my ints that way.

humm... I actually don't know. I normally use line return instead of space, so, I simply do readline().

You can:

1) read() and decide if you want to include as part of the integer or not.

2) readblock() a length and split the string using the space character.

Both are not as efficient as your example though.

If you have the the authority to change the file format, using line return would make your problem a lot easier. Or actually it is best to use binary file as it will save tons of data size and makes reading the HDD much more effective.

@magicalclick, I've tried Read() and it gets a single char, the problem with this and the ReadBlock() approach is that I actually don't know the length of each number and they could differ in length. I could certainly just read the entire line and the break it up using space but that wouldn't be as elegant as the C++ solution.

Could there be a method which takes a stream of characters & optional function which can eg. replace newline or string with space & a delimiter (like space) and when executed it immediately starts yielding new streams (without waiting for the first delimiter to occur) that can be read up to the delimiter (or current position [up to that delimiter specific to the yield] when del. not yet encountered) and if none is encountered, it would just yield the whole original stream (wrapped) with some flag indicating that no delimiters were found?

I guess if you want performance and simplicity in C# expect to write a bunch of code. And still it won't perform like C++. This whole "C++ reneissance" is a bunch of non-sense, for now. I'm still out for a language that's written and reads more like C# but if I want I can paste in some C++ code and link native libs etc without perf penalty. Is that too much to ask?