473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

Parsing text files

Ron
Hi,

I need to parse text (ie. created in Notepad) files for numbers (doubles).

In Borland C++ Builder the following works:

if(!InVect.is_open())
{
InVect.open(TxtFileName.c_str()) ;
}

for(int Index = 1; Index < ILayerSize; Index++)
{
InVect >> InputVect[Index] ;
etc
}

('InputVect' is an array of doubles.)

I have been trying to perform the same kind of operation in C# but without
success so far.

Any help much appreciated,
Ron.
Nov 16 '05 #1
4 9408
Hi Ron,

Is the file ordered in any way, for instance only numbers separated with a
linebreak?
In that case you can simply read the whole file as a string, split the
string at each linebreak or other separator, and then convert all entries
in the resulting string to doubles.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Ron
Managed to do it this way but it seems a bit long winded:

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
string TrainFileName = openFileDialog1.FileName;
if (File.Exists(TrainFileName))
{
StreamReader s = new StreamReader(TrainFileName);
ArrayList strs = new ArrayList();
string temp = null;
string st = s.ReadToEnd();
int strInd = 0;
for (int i = 0; i < st.Length; i++)
{
if ((st[i] >= '0' && st[i] <= '9')
|| st[i] == '.' || st[i] == '+'
|| st[i] == '-')
{
temp += st[i];
}
else
{
strs.Add(temp);
temp = null;
}

}
foreach(string a in strs)
{
double r;
if(double.TryParse(a,out r))
richTextBox1.Text += r.ToString() + '\n'; // These
values will be assigned to an array of doubles
}

s.Close();
}
}

Nov 16 '05 #3
Wes
Hello Ron,

Assuming you have a text file with one double per line you could do the following.

ArrayList nums = new ArrayList();
string filename = "";
if (File.Exists(filename))
{
using (TextReader tr = new StreamReader(filename))
{
string line;
while ((line = tr.ReadLine()) != null)
{
Double d;
if (Double.TryParse(line, out d))
nums.Add(d);
}
}
}

If you just have a text file with double delimited by white space perhaps using a regular expression to split them would be easist

if (File.Exists(filename))
{
using (TextReader tr = new StreamReader(filename))
{
string[] split = Regex.Split(tr.ReadToEnd(), @"(?m)\s+");

foreach (string num in split)
{
Double d;
if (Double.TryParse(num, out d))
nums.Add(d);
}
}
}

HTH
Wes Haggard
http://weblogs.asp.net/whaggard/

Managed to do it this way but it seems a bit long winded:

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
string TrainFileName = openFileDialog1.FileName;
if (File.Exists(TrainFileName))
{
StreamReader s = new StreamReader(TrainFileName);
ArrayList strs = new ArrayList();
string temp = null;
string st = s.ReadToEnd();
int strInd = 0;
for (int i = 0; i < st.Length; i++)
{
if ((st[i] >= '0' && st[i] <= '9')
|| st[i] == '.' || st[i] == '+'
|| st[i] == '-')
{
temp += st[i];
}
else
{
strs.Add(temp);
temp = null;
}
}
foreach(string a in strs)
{
double r;
if(double.TryParse(a,out r))
richTextBox1.Text += r.ToString() + '\n'; //
These
values will be assigned to an array of doubles
}
s.Close();
}
}


Nov 16 '05 #4
Ron
Thanks Wes,

The regular expression stuff is new to me.

Your routine looks ideal for my purposes - I'll give it a try!
Regards,
Ron.
If you just have a text file with double delimited by white space perhaps
using a regular expression to split them would be easist

if (File.Exists(filename))
{
using (TextReader tr = new StreamReader(filename))
{
string[] split = Regex.Split(tr.ReadToEnd(), @"(?m)\s+");

foreach (string num in split)
{
Double d;
if (Double.TryParse(num, out d))
nums.Add(d);
}
}
}

HTH
Wes Haggard
http://weblogs.asp.net/whaggard/

Nov 16 '05 #5

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

Similar topics

2
by: Michael Hogan | last post by:
I want to pars a playlist file for three different varibles, so I can save them as mp3 files. I am using: strTEMPURL = GetUrlSource(Text1.Text) to put the entire .pls file into a strTEMPURL...
8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
4
by: Marian Jancar | last post by:
Hi, Is there a module for parsing spec files available? Marian -- -- Best Regards,
35
by: .:mmac:. | last post by:
I have a bunch of files (Playlist files for media player) and I am trying to create an automatically generated web page that includes the last 20 or 30 of these files. The files are created every...
0
by: david | last post by:
Hi all, I am trying to do something which would seem to be simple. I need to parse message files (.msg extensions) in order to find certain words in the content of the message. I thought that the...
9
by: Hemang Shah | last post by:
Hello fellow Coders! ok, I"m trying to write a very simple application in C#. (Yes its my first program) What I want to do is : 1) Open a binary file 2) Search this file for a particular...
4
by: Neil.Smith | last post by:
I can't seem to find any references to this, but here goes: In there anyway to parse an html/aspx file within an asp.net application to gather a collection of controls in the file. For instance...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
3
by: davebaty | last post by:
I'm relatively new to VB programming (VB 2005), and have come across a problem parsing complex text files. Basically I have a file which has lines something like the following: max_gross_weight...
22
by: JJ | last post by:
Whats the best way for me to pull out records from a tab delimited text file? Or rather HOW do I parse the text, knowing that the tabs are field delimiters and a return (I image) signifies a new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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
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...

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.