473,509 Members | 2,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 26 '07 #1
13 2902
No, there is not the same method as fscanf.
You need to read the line and use pattern matching to parse line on types

--
WBR, Michael Nemtsev [.NET/C# MVP].
Blog: http://spaces.live.com/laflour

"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
Nov 27 '07 #2
//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?
Nov 27 '07 #3
On Nov 26, 8:54 pm, Lev Elbert <elbert...@hotmail.comwrote:
//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.
Nov 27 '07 #4
Lev Elbert <el*******@hotmail.comwrote:
//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
Nov 27 '07 #5
On 27 Nov., 05:05, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:
On Nov 26, 8:54 pm, Lev Elbert <elbert...@hotmail.comwrote:
//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 ;)

Kevin Wienhold
Nov 27 '07 #6
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
If you're not too concerned about parse errors (e.g. willing to accept exceptions),
this could be done like this:

double[] numbers = Array.ConvertAll(RegEx.Split(line, "\\s+")), Double.Parse);

>
Marathoner

Nov 27 '07 #7
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
JLev Elbert <el*******@hotmail.comwrote:
J>
>//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?
JIs a regular expression + TextReader.ReadLine really so hard?
J>
Nov 27 '07 #8
How would you use regular expression?

Marathoner

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Lev Elbert <el*******@hotmail.comwrote:
>//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

Nov 27 '07 #9
On Nov 27, 3:14 pm, "marathoner" <rajk2...@msn.com.invalidwrote:
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
Nov 27 '07 #10
On Nov 27, 8:30 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Nov 27, 3:14 pm, "marathoner" <rajk2...@msn.com.invalidwrote:
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
The fact is that working with operator >in C++ was facinatingly easy
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.
Nov 28 '07 #11
je**********@gmail.com <je**********@gmail.comwrote:
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 :)

The fact is that working with operator >in C++ was facinatingly easy
for beginning programmers.
It's still an abuse of operator overloading, IMO.
I have worked with both philosophies for a
long while each. I prefer >simply because it says a lot more with
less code.
You could say a lot with little code by naming methods "A" and "B"
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
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.
On the other hand, it's usually more efficient to grab a whole buffer's
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
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.
Indeed.
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.
Yes, in general .NET has been very well designed - it's learned lessons
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
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.
True. I still object to the overloading of >on principle though :)
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
Nov 28 '07 #12

"KWienhold" <he******@trashmail.netwrote in message
news:7e**********************************@x69g2000 hsx.googlegroups.com...
On 27 Nov., 05:05, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:
>On Nov 26, 8:54 pm, Lev Elbert <elbert...@hotmail.comwrote:
//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 ;)
I hope that your wink indicates <sarcasm></sarcasmaround your post.

But it turns out that C# can overload << and >just fine.
>
Kevin Wienhold

Nov 29 '07 #13
On 29 Nov., 18:54, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"KWienhold" <hedov...@trashmail.netwrote in message

news:7e**********************************@x69g2000 hsx.googlegroups.com...


On 27 Nov., 05:05, "jehugalea...@gmail.com" <jehugalea...@gmail.com>
wrote:
On Nov 26, 8:54 pm, Lev Elbert <elbert...@hotmail.comwrote:
//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 ;)

I hope that your wink indicates <sarcasm></sarcasmaround your post.

But it turns out that C# can overload << and >just fine.


Kevin Wienhold- Zitierten Text ausblenden -

- Zitierten Text anzeigen -- Zitierten Text ausblenden -

- Zitierten Text anzeigen -
Yeah, it was supposed to be humerous, in retrospect, it probably
wasn't ;)

Kevin Wienhold
Nov 30 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6191
by: Sergio Otoya | last post by:
Hi all, I have a potentially simple question... I need to keep the value of a field between form posts? I was thinking of using the equivalent of Request.Form("Field1") in javascript to set...
4
2490
by: Hai Nguyen | last post by:
I'm learning C sharp and do not like vb much. I'm creatiing a wepage using panel to test myself. I tried to use these code below, which is written in VB, and to transform them to c sharp but I got...
1
3908
by: Pieter | last post by:
I want to be able to split regular MAX_PATH length filenames and 32K UNICODE filenames using the \\?\ naming convention. Are there any equivalents to _tsplitpath() for long names, or am I left to...
9
4030
by: Alan Silver | last post by:
Hello, I'm converting some old VB6 code to use with ASP.NET and have come unstuck with the Asc() function. This was used in the old VB6 code to convert a character to its ASCII numeric...
4
6674
by: pcnerd | last post by:
I've been playing with "classic" VB since version 3. I have VB6 Learning Edition. Recently, I wanted to try VB.NET. I got a beginner's book with a CD with the software & installed it. There are...
89
5948
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
8
1383
by: Jon Paal | last post by:
what is vb equivalent to this C# code ? converters failed to handle this "using (SQLiteTransaction mytransaction = myconnection.BeginTransaction())"
2
3725
elamberdor
by: elamberdor | last post by:
Hi All! Well, i'm modifying a dynamic map, with lat and long datapoints, my problem is it loads in text perfectly onto exact points I specify on the map, ..well now I want to load in...
18
15858
by: Csaba Gabor | last post by:
Is there a straightforward way of implementing a PHP equivalent to window.setTimeout? In a javascript web app, it's often the case that things are done on an event driven basis. For example,...
68
4552
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7136
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.