Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Reading a text file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nick - 27 Aug 2006 17:11 GMT
Hiya,

Am having a few problems with StreamReader.

I'm trying to open a text file, and read 5 columns, and put them into an
array, and then do a calculation and read the next line of data, and do a
calc etc.

I can open the text file and display it fine, but cant seem to get each line
into an array to do the maths :(

Any help appreciated,
Rgds,
Nick
Signature

www.freefonesoftware.co.uk

Tasos Vogiatzoglou - 27 Aug 2006 18:02 GMT
Nick, the columns should be separated by a means of a character, like a
tab. To get the strings and do the calculations you could try something
like the following

String[] tokens = line.Split("\t");
int[] numbers = new int[tokens.Length];

for (int i=0;i<numbers.Length;i++) {
try {
   numbers[i] = Convert.ToInteger(tokens[i]);
} catch (Exception ex) {
   numbers[i] = -1; //this in case a number is malformed
}
}

and you will have your numbers in number array.

Regards,
Tasos

> Hiya,
>
[quoted text clipped - 10 lines]
> Rgds,
> Nick
Nick - 27 Aug 2006 18:24 GMT
Thank you for your reply, the data in question looks like

2934477 LLWL 1.11 £0.00 (R) T[19.8  -   1.11 19.8]

I want the 1.11, then the 4 numbers in the [] brackets, the - would
sometimes have a number, sometimes not.

How would I be able to put these 4 numbers into an array exactly? The 4
numbers are odds, and the 3rd column is the winning hand.

Rgds,
Nick

> Nick, the columns should be separated by a means of a character, like a
> tab. To get the strings and do the calculations you could try something
[quoted text clipped - 12 lines]
>
> and you will have your numbers in number array.
Tasos Vogiatzoglou - 27 Aug 2006 19:00 GMT
Nick,

String[] tokens = line.Split("[");
String winHand = tokens[0].Split(" ")[2];
String[] numbers = tokens[1].Trim("]").Split[" "];

The winHand var has the winning hand.
The numbers array has the four numbers .

Regards,
Tasos

> Thank you for your reply, the data in question looks like
>
[quoted text clipped - 25 lines]
> >
> > and you will have your numbers in number array.
Nick - 28 Aug 2006 00:37 GMT
Any chance you could do some code I can copy/paste and alter slightly to fit
in, as am finding this very fustrating, and usually learn by thoroughly
reading someone elses code and re-applying it lol. Will send you a few $$
for your troubles if you can,

Rgds,
Nick

Nick,

String[] tokens = line.Split("[");
String winHand = tokens[0].Split(" ")[2];
String[] numbers = tokens[1].Trim("]").Split[" "];

The winHand var has the winning hand.
The numbers array has the four numbers .

Regards,
Tasos

Nick wrote:
> Thank you for your reply, the data in question looks like
>
[quoted text clipped - 25 lines]
> >
> > and you will have your numbers in number array.
Tasos Vogiatzoglou - 28 Aug 2006 10:45 GMT
This is not the point of learning...

you should try to develop the code yourself and face all the problems.

I've just provided the samples in order to show you the way to go, not
to solve your problem.

Regards,
Tasos

> Any chance you could do some code I can copy/paste and alter slightly to fit
> in, as am finding this very fustrating, and usually learn by thoroughly
[quoted text clipped - 46 lines]
> > >
> > > and you will have your numbers in number array.
Otis Mukinfus - 27 Aug 2006 18:33 GMT
>Hiya,
>
[quoted text clipped - 10 lines]
>Rgds,
>Nick

Nick,

Does the line have delimiters in it?  Are the columns you want to use for the
calculations separated by commas or tabs or something like that?

If the line you are reading you can use the String.Split method to do exactly
what you are wanting to do.

Here is an example:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = "10,21,10,17";

            string[] cols = line.Split(new char[] { ',' });

            int sum =
                Convert.ToInt32(cols[0]) +
                Convert.ToInt32(cols[1]) +
                Convert.ToInt32(cols[2]) +
                Convert.ToInt32(cols[3]);

            Console.Write("the sum is {0}", sum);
            Console.ReadLine();
        }
    }
}
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.