Reading one line at a time and parsing - StreamReader HELP!?

  • Thread starter Thread starter BlueOysterCult
  • Start date Start date
B

BlueOysterCult

Hello all
I have a project that I have to read the file - which I have done I
can see the file output. I then need to read the line - parse the
line into three separate values (there are 3 = name age and gender)


static void DoPartD()
{

FileInfo theSourceFile = new FileInfo(@"c:\\Documents and
Settings\\Rob\\School\\CIS 252\\Ass2\\people.DAT");
StreamReader reader = theSourceFile.OpenText();
StreamWriter writer = new StreamWriter(@"c:\\Documents and
Settings\\Rob\\School\\CIS 252\\Ass2\\people.BAK",false);

string text;

do
{
text = reader.ReadLine();
writer.WriteLine(text);
Console.WriteLine(text);
} while (text != null);
reader.Close();
writer.Close();
}

I am not sure how to read just one line at a time to parse them

like I said, I can see all the lines when
Console.writeline(text)......

It is delimited with a semi colon

Thanks

Rob
 
BlueOysterCult said:
I have a project that I have to read the file - which I have done I
can see the file output. I then need to read the line - parse the
line into three separate values (there are 3 = name age and gender)

static void DoPartD()
{

FileInfo theSourceFile = new FileInfo(@"c:\\Documents and
Settings\\Rob\\School\\CIS 252\\Ass2\\people.DAT");
StreamReader reader = theSourceFile.OpenText();
StreamWriter writer = new StreamWriter(@"c:\\Documents and
Settings\\Rob\\School\\CIS 252\\Ass2\\people.BAK",false);

string text;

do
{
text = reader.ReadLine();
writer.WriteLine(text);
Console.WriteLine(text);
} while (text != null);
reader.Close();
writer.Close();
}

I am not sure how to read just one line at a time to parse them

You *are* already reading just one line at a time - although you ought
to break out of your loop as soon as text is null, rather than calling
writer.WriteLine(null) and Console.WriteLine (null) - it doesn't matter
for those two calls, but for other calls which use text, it may well.
like I said, I can see all the lines when
Console.writeline(text)......

It is delimited with a semi colon

So within your loop, where you've got writer.WriteLine and
Console.WriteLine, put the parsing code, which should probably use
String.Split.
 
to read one line at a time u can use the following code


using System.IO;
using System;

public class cls
{

public static void Main()
{

FileInfo fl = new FileInfo("file.txt");
StreamReader reader = fl.OpenText();
while(reader.Peek()>=0)
{
Console.WriteLine(reader.ReadLine().ToString());
}

reader.Close();

}

}
BlueOysterCult said:
Hello all
I have a project that I have to read the file - which I have done I
can see the file output. I then need to read the line - parse the
line into three separate values (there are 3 = name age and gender)


static void DoPartD()
{

FileInfo theSourceFile = new FileInfo(@"c:\\Documents and
Settings\\Rob\\School\\CIS 252\\Ass2\\people.DAT");
StreamReader reader = theSourceFile.OpenText();
StreamWriter writer = new StreamWriter(@"c:\\Documents and
Settings\\Rob\\School\\CIS 252\\Ass2\\people.BAK",false);

string text;

do
{
text = reader.ReadLine();
writer.WriteLine(text);
Console.WriteLine(text);
} while (text != null);
reader.Close();
writer.Close();
}

I am not sure how to read just one line at a time to parse them

like I said, I can see all the lines when
Console.writeline(text)......

It is delimited with a semi colon

Thanks

Rob



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
yeah you are right.. bad habit.. I got used to use ToString() too much..
that too when I write code using notepad.. I tend to use it more often..
since i wouldnt have the help if IDE..
 
ugggg

I have tried everything - nothing is working

I can't get the syntax right-- I know its something.Split({";"});

etc but nothing is working
Forgive me - I am new to C#

R
 
myString.Split(';'); // This what you need?

This uses the params char[] sep overload. With params, the compiler is able to
accept an expanded parameter list to the function and shrink that down into the
required array for you. To explicitly package a character array you could use:

myString.Split(new char[] {';'});


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

BlueOysterCult said:
ugggg

I have tried everything - nothing is working

I can't get the syntax right-- I know its something.Split({";"});

etc but nothing is working
Forgive me - I am new to C#

R




http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
BlueOysterCult said:
I have tried everything - nothing is working

I can't get the syntax right-- I know its something.Split({";"});

etc but nothing is working
Forgive me - I am new to C#

Have you looked closely at the documentation for String.Split, and the
example given?
 
Back
Top