Hi,
I need to parse text (ie. created in Notepad) files for numbers (doubles).
In Borland C++ Builder the following works:
if(!InVect.is_o pen())
{
InVect.open(Txt FileName.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. 4 9427
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]
Managed to do it this way but it seems a bit long winded:
if(openFileDial og1.ShowDialog( ) == DialogResult.OK )
{
string TrainFileName = openFileDialog1 .FileName;
if (File.Exists(Tr ainFileName))
{
StreamReader s = new StreamReader(Tr ainFileName);
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.TryPa rse(a,out r))
richTextBox1.Te xt += r.ToString() + '\n'; // These
values will be assigned to an array of doubles
}
s.Close();
}
}
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(fi lename))
{
using (TextReader tr = new StreamReader(fi lename))
{
string line;
while ((line = tr.ReadLine()) != null)
{
Double d;
if (Double.TryPars e(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(fi lename))
{
using (TextReader tr = new StreamReader(fi lename))
{
string[] split = Regex.Split(tr. ReadToEnd(), @"(?m)\s+");
foreach (string num in split)
{
Double d;
if (Double.TryPars e(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(openFileDial og1.ShowDialog( ) == DialogResult.OK ) { string TrainFileName = openFileDialog1 .FileName; if (File.Exists(Tr ainFileName)) { StreamReader s = new StreamReader(Tr ainFileName); 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.TryPa rse(a,out r)) richTextBox1.Te xt += r.ToString() + '\n'; // These values will be assigned to an array of doubles } s.Close(); } }
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(fi lename)) { using (TextReader tr = new StreamReader(fi lename)) { string[] split = Regex.Split(tr. ReadToEnd(), @"(?m)\s+");
foreach (string num in split) { Double d; if (Double.TryPars e(num, out d)) nums.Add(d); } } }
HTH Wes Haggard http://weblogs.asp.net/whaggard/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 varible.
I want to grab .pls files from shoutcast and break them into varibles.
----------------Begin winamp.pls file-------------------
|
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 $
Last-Modified: $Date: 2003/10/28 19:48:44 $
Author: A.M. Kuchling <amk@amk.ca>
Status: Draft
Type: Standards Track
|
by: Marian Jancar |
last post by:
Hi,
Is there a module for parsing spec files available?
Marian
--
--
Best Regards,
|
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 week and are named XX-XX-XX.ASX
where the X's represent the date i.e. 05-22-05.asx
The files are a specific format and will always contain tags like the
following:
<TITLE>My media file title</TITLE>
<AUTHOR>Media file author</AUTHOR>
<Ref href =...
|
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 simplest way would be to parse these
files as if they were text files (using Stream Reader).
The problem comes that some of these messages are sent in
an HTML format, so parsing them with a stream reader omits
a great deal of the text, or turns...
| |
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 string.
3) Close the file
|
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 what I'm trying to do is upload a html file onto the web
server, convert it to aspx file and then parse it for input
tags/controls, which in turn will become fields in a newly created
database table.
Clearly when the aspx file is called the...
|
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 the file) with the location.
And for a particular section I parse only that section.
The file is something like,
.... DATAS
|
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 = 150000 // (pounds)
empty_weight = 74170 // (pounds)
max_number_of_stations = 50
station_load.0 = "170.0, 41.0, -1.5, 0.0, Pilot" //Weight (lbs),
station_load.1 = ...
|
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 record
?
JJ
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |