473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3719
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.L ength;i++) {
try {
numbers[i] = Convert.ToInteg er(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******** *************@i 42g2000cwa.goog legroups.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.L ength;i++) {
try {
numbers[i] = Convert.ToInteg er(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.Collecti ons.Generic;
using System.Text;

namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
string line = "10,21,10,1 7";

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.ReadLin e();
}
}
}
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******** *************@i 42g2000cwa.goog legroups.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.L ength;i++) {
try {
numbers[i] = Convert.ToInteg er(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.goog legroups.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******** *************@i 42g2000cwa.goog legroups.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.L ength;i++) {
try {
numbers[i] = Convert.ToInteg er(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.goog legroups.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******** *************@i 42g2000cwa.goog legroups.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.L ength;i++) {
try {
numbers[i] = Convert.ToInteg er(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
12752
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
7060
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 file... Thank you to all of you who can help me with this one...
19
10387
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 text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
0
1757
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 Process=Interim Level=10 Technique=Fletching Knowledge=Woodworking Device=Sawhorse Primary components=Refined Maple
1
6764
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 listen up; Reports, the way I look at them, all present data downwards, in this way; TITLE data
50
5048
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 -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
2
2501
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() { OperatingSystem os = Environment.OSVersion; AppDomain ad = Thread.GetDomain();
4
3308
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 errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent the file to wordpad, it shows
4
12812
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 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
3
2839
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 (!file.eof ()) { do {
0
9716
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
9595
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,...
0
10604
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...
1
10359
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
9177
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...
0
5536
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...
1
4314
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
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
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...

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.