473,406 Members | 2,281 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,406 software developers and data experts.

Reading a text file

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
--
www.freefonesoftware.co.uk
Aug 27 '06 #1
6 3700
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
Nick wrote:
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
--
www.freefonesoftware.co.uk
Aug 27 '06 #2
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

"Tasos Vogiatzoglou" <tv*****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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.

Aug 27 '06 #3
On Sun, 27 Aug 2006 16:12:17 GMT, "Nick" <na****@hotmail.comwrote:
>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
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
Aug 27 '06 #4
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

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

"Tasos Vogiatzoglou" <tv*****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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.
Aug 27 '06 #5
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
"Tasos Vogiatzoglou" <tv*****@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
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

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

"Tasos Vogiatzoglou" <tv*****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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.

Aug 27 '06 #6
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

Nick wrote:
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
"Tasos Vogiatzoglou" <tv*****@gmail.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
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

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

"Tasos Vogiatzoglou" <tv*****@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Nick, the columns should be separated by a means of a character, likea
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.
Aug 28 '06 #7

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

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
0
by: Eric Lilja | last post by:
Hello, I have a text file that contains a number of entries describing a recipe. Each entry consists of a number of strings. Here's an example file with only one entry (recipe): Name=Maple Quill...
1
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
4
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...

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.