|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using "fscan" Equivalent in C#I would like to read the following entries of mixed data types from a ascii
text file using C#. This can be easily performed in C using fscanf. Is there an equivalent function in C#? 1 2 1201 1 -0.417597000000000D+06 0.129600000000000D+06 0.0 0.753000000000000D+03 0.198800000000000D+04 Marathoner No, there is not the same method as fscanf.
You need to read the line and use pattern matching to parse line on types Show quote "marathoner" wrote: > I would like to read the following entries of mixed data types from a ascii > text file using C#. This can be easily performed in C using fscanf. Is > there an equivalent function in C#? > > > 1 2 1201 1 -0.417597000000000D+06 0.129600000000000D+06 > 0.0 0.753000000000000D+03 0.198800000000000D+04 > > > Marathoner > > > //You need to read the line and use pattern matching to parse line on
types wouldn't' it be good if such task could be done simpler? On Nov 26, 8:54 pm, Lev Elbert <elbert***@hotmail.com> wrote:
> //You need to read the line and use pattern matching to parse line on People who have been around C and C++ who know about the wonders of >>> types > > wouldn't' it be good if such task could be done simpler? operators and scanfs find it hard to believe there is not a similar, easy facility in Java and C#. There are a lot of small projects that simulate it as best as possible. I would recommend a search on CodeProject.
Show quote
On 27 Nov., 05:05, "jehugalea***@gmail.com" <jehugalea***@gmail.com> Actually, C# does have bit-shifting, even using the same operator ;)wrote: > On Nov 26, 8:54 pm, Lev Elbert <elbert***@hotmail.com> wrote: > > > //You need to read the line and use pattern matching to parse line on > > types > > > wouldn't' it be good if such task could be done simpler? > > People who have been around C and C++ who know about the wonders of >> > operators and scanfs find it hard to believe there is not a similar, > easy facility in Java and C#. There are a lot of small projects that > simulate it as best as possible. I would recommend a search on > CodeProject. Kevin Wienhold
Show quote
"KWienhold" <hedov***@trashmail.net> wrote in message I hope that your wink indicates <sarcasm></sarcasm> around your post.news:7e28897f-571e-4996-b86d-8f64bfa8c0e3@x69g2000hsx.googlegroups.com... > On 27 Nov., 05:05, "jehugalea***@gmail.com" <jehugalea***@gmail.com> > wrote: >> On Nov 26, 8:54 pm, Lev Elbert <elbert***@hotmail.com> wrote: >> >> > //You need to read the line and use pattern matching to parse line on >> > types >> >> > wouldn't' it be good if such task could be done simpler? >> >> People who have been around C and C++ who know about the wonders of >> >> operators and scanfs find it hard to believe there is not a similar, >> easy facility in Java and C#. There are a lot of small projects that >> simulate it as best as possible. I would recommend a search on >> CodeProject. > > Actually, C# does have bit-shifting, even using the same operator ;) But it turns out that C# can overload << and >> just fine. Show quote > > Kevin Wienhold Lev Elbert <elbert***@hotmail.com> wrote:
> //You need to read the line and use pattern matching to parse line on Is a regular expression + TextReader.ReadLine really so hard?> types > > wouldn't' it be good if such task could be done simpler? -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk Hello Jon Skeet [C# MVP],
It seems very crazy after F# pattern matching/active pattern features , isn't it ;) --- WBR, Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour "The greatest danger for most of us is not that our aim is too high and we miss it, but that it is too low and we reach it" (c) Michelangelo J> Lev Elbert <elbert***@hotmail.com> wrote: J> >> //You need to read the line and use pattern matching to parse line on J> Is a regular expression + TextReader.ReadLine really so hard?>> types >> >> wouldn't' it be good if such task could be done simpler? >> J> How would you use regular expression?
Marathoner Show quote "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MPG.21b5d8cb9ad1ab5066e@msnews.microsoft.com... > Lev Elbert <elbert***@hotmail.com> wrote: >> //You need to read the line and use pattern matching to parse line on >> types >> >> wouldn't' it be good if such task could be done simpler? > > Is a regular expression + TextReader.ReadLine really so hard? > > -- > Jon Skeet - <sk***@pobox.com> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > World class .NET training in the UK: http://iterativetraining.co.uk On Nov 27, 3:14 pm, "marathoner" <rajk2***@msn.com.invalid> wrote: Use the regex to capture the appropriate groups (if it's not as simple> How would you use regular expression? as just splitting by a common delimiter - if it is, that's fine) and then int.Parse etc to convert those group values into the types you need. If you can get away with just using string.Split and then parsing each part separately, so much the better :) Jon
Show quote
On Nov 27, 8:30 am, "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote: The fact is that working with operator >> in C++ was facinatingly easy> On Nov 27, 3:14 pm, "marathoner" <rajk2***@msn.com.invalid> wrote: > > > How would you use regular expression? > > Use the regex to capture the appropriate groups (if it's not as simple > as just splitting by a common delimiter - if it is, that's fine) and > then int.Parse etc to convert those group values into the types you > need. > > If you can get away with just using string.Split and then parsing each > part separately, so much the better :) > > Jon for beginning programmers. I have worked with both philosophies for a long while each. I prefer >> simply because it says a lot more with less code. However, most programmers don't have the time or the training to fully understand >> overloading for their custom types. C+ + programmers see reading an entire line as a bit like filling your mouth with more than you can chew. I have always felt like I am taking more than I need up-front and then breaking it into pieces and then converting all the pieces to the right type and then finally using them for their intended purpose. A lot of input isn't that complicated, in general. However, in light of things, input is rare. If you are reading data files, then usually there is a set format and it is usually separated by lines anyway. Regex and Int32.Parse is a lot more manual but a lot more safe. >> will fail to read something and go on tra-la-la-ing. scanf's are just plain type-unsafe and hard for most people to understand. It is possible for C++ IO to throw errors on bad data reads, however, it is an advanced topic. The one failure of the C++ IO is its complexity. I believe it can be learned by buying a good book on the STL, however, it isn't something you are going to retain overnight. The .NET IO classes are very intuitive and work well together. There is no law against pulling in input characters-at-a-time and converting them to numbers manually. fscanf is really just a big if- else inside a loop - anyone could write a trimmed-down version. I like C#'s method and I like C++'s method. A lot of research, history and ingenuity went into both of them and they should be respected by anyone who appreciates good code. It's really not a matter of which way's best or which way is sufficient. It is a matter of changing philosophies. jehugalea***@gmail.com <jehugalea***@gmail.com> wrote:
> > > How would you use regular expression? It's still an abuse of operator overloading, IMO.> > > > Use the regex to capture the appropriate groups (if it's not as simple > > as just splitting by a common delimiter - if it is, that's fine) and > > then int.Parse etc to convert those group values into the types you > > need. > > > > If you can get away with just using string.Split and then parsing each > > part separately, so much the better :) > > The fact is that working with operator >> in C++ was facinatingly easy > for beginning programmers. > I have worked with both philosophies for a You could say a lot with little code by naming methods "A" and "B" > long while each. I prefer >> simply because it says a lot more with > less code. instead, too - it wouldn't be a good idea. >> is defined in the language spec to be a bit shifting operation, which reading data from a stream certainly isn't. I'm really glad C# hasn't gone this route. > However, most programmers don't have the time or the On the other hand, it's usually more efficient to grab a whole buffer's > training to fully understand >> overloading for their custom types. C+ > + programmers see reading an entire line as a bit like filling your > mouth with more than you can chew. I have always felt like I am taking > more than I need up-front and then breaking it into pieces and then > converting all the pieces to the right type and then finally using > them for their intended purpose. A lot of input isn't that > complicated, in general. worth of data and then process it, than to request one byte at a time from the input stream. > However, in light of things, input is rare. If you are reading data Indeed.> files, then usually there is a set format and it is usually separated > by lines anyway. Regex and Int32.Parse is a lot more manual but a lot > more safe. >> will fail to read something and go on tra-la-la-ing. > scanf's are just plain type-unsafe and hard for most people to > understand. > It is possible for C++ IO to throw errors on bad data Yes, in general .NET has been very well designed - it's learned lessons > reads, however, it is an advanced topic. The one failure of the C++ IO > is its complexity. I believe it can be learned by buying a good book > on the STL, however, it isn't something you are going to retain > overnight. The .NET IO classes are very intuitive and work well > together. from C++ and Java. It's a shame that it didn't get date+time support right to start with; I believe it's a lot better in 3.5 but I haven't looked in detail. > There is no law against pulling in input characters-at-a-time and True. I still object to the overloading of >> on principle though :) > converting them to numbers manually. fscanf is really just a big if- > else inside a loop - anyone could write a trimmed-down version. I like > C#'s method and I like C++'s method. A lot of research, history and > ingenuity went into both of them and they should be respected by > anyone who appreciates good code. It's really not a matter of which > way's best or which way is sufficient. It is a matter of changing > philosophies. Having a *method* which you pass a format to is a reasonable idea. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk > I would like to read the following entries of mixed data types from a If you're not too concerned about parse errors (e.g. willing to accept exceptions), > ascii text file using C#. This can be easily performed in C using > fscanf. Is there an equivalent function in C#? > > 1 2 1201 1 -0.417597000000000D+06 > 0.129600000000000D+06 0.0 0.753000000000000D+03 > 0.198800000000000D+04 this could be done like this: double[] numbers = Array.ConvertAll(RegEx.Split(line, "\\s+")), Double.Parse); Show quote > > Marathoner > |
|||||||||||||||||||||||