473,783 Members | 2,354 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.4175970000000 00D+06 0.1296000000000 00D+06
0.0 0.7530000000000 00D+03 0.1988000000000 00D+04
Marathoner
Nov 26 '07 #1
13 2931
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.4175970000000 00D+06 0.1296000000000 00D+06
0.0 0.7530000000000 00D+03 0.1988000000000 00D+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...@hotm ail.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*******@hotm ail.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.Read Line really so hard?

--
Jon Skeet - <sk***@pobox.co m>
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...@g mail.com" <jehugalea...@g mail.com>
wrote:
On Nov 26, 8:54 pm, Lev Elbert <elbert...@hotm ail.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.4175970000000 00D+06
0.1296000000000 00D+06 0.0 0.7530000000000 00D+03
0.1988000000000 00D+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.ConvertAl l(RegEx.Split(l ine, "\\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*******@hotm ail.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.Read Line really so hard?
J>
Nov 27 '07 #8
How would you use regular expression?

Marathoner

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
Lev Elbert <el*******@hotm ail.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.Read Line really so hard?

--
Jon Skeet - <sk***@pobox.co m>
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.c om.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

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

Similar topics

2
6200
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 the value of the field on the body onload event? is this possible in javascript? Is there are simple way to do this?
4
2512
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 hard time to understand vb syntax. I don't know if anyone in here can point out which is the equivalent object used in c sharp. Translate these two lines to C sharp: Sub Next_Click(Sender As Object, e As EventArgs) Select Case...
1
3926
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 writing my own split code? Thanks Pieter
9
4050
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 equivalent. Is there such a function available in C#? I can see that VB.NET has one, but I couldn't see how to get at it in C#. For example, if I have ...
4
6695
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 things that I like about VB.NET & there are things that I don't like. I like to do graphics like plotting math functions & fractals & stuff. VB has Pset. In VB6, it's easy to plot a pixel. Just use Pset. In VB.NET, it isn't. I have to create a...
89
6079
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 used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
8
1400
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
3744
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 icons(images) instead of text. (yes i'm being difficult) and i'd like the text to load into a static placeholder symbol instead. Problem is, I can get images to load in a separate file, text to load in a separate file, and text to appear in a static...
18
15928
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, html elements might have things like onclick="buttonClicked()" to indicate that the function buttonClicked should be run when that element is clicked. The analogue can be done in PHP (on Windows) using com_event_sink. In other words, you can...
68
4653
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 temporary but it was stated that it would not. This has come up in an irc channel but I can not find the original thread, nor can I get any code to work. Foo& Bar( int Val ) { return Foo( Val ); }
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.