help with reading fields in a text file

  • Thread starter Thread starter Vicky via DotNetMonster.com
  • Start date Start date
V

Vicky via DotNetMonster.com

Please help with reading fields in a text file.
I need to get the index name, (ticker field in text file),
Please see one file as an example (There are actually 1000 files)

If we open the file, the text includes Header and data
TICKER>,<PER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>,<OPENINT>
..A1DOW,M,19980630,000000,264.2900,264.9700,250.8000,264.2900,0,0
..A1DOW,M,19980731,000000,260.3700,276.5900,260.3700,260.3700,0,0

I need to read the ticker A1DOW and M to console first, (later I will
compare the ticker with existing DB, and load the data.

Thank you for any info, which method I should look into it?
I really appreciated.

Vicky
 
Hi,

Take a look at the CSV data provider from opennetcf.org

cheers,
 
A very easy way to read these values from the file will be to use the
String.Split method like so:

StreamReader sr = new StreamReader(filePath);

string currentLine = null;

do
{
currentLine = sr.ReadLine();
string[] data = currentLine.Split(',');

string val1 = data[0];
string val2 = data[1];
// etc.

}while(currentLine != null);

Hope this helps.
Cordell Lawrence
Teleios Systems Ltd.
 
Cordell,

Thank you so much for your help, I will test it and let you know.

Vicky
 
Cordell,

I tested, everything is working besides I saw error msg said
An exception 'System.NullReferenceException' has occurred in
OpenText.exe

Object reference not set to an instant of an object.

but it still runs, I add the code to print val1 and val2 to the console
so I could see the result, it did print to the console.

Do you have any hint how to modify the code without the msg.

Thanks!
 
Vicky via DotNetMonster.com said:
I tested, everything is working besides I saw error msg said
An exception 'System.NullReferenceException' has occurred in
OpenText.exe

Object reference not set to an instant of an object.

but it still runs, I add the code to print val1 and val2 to the console
so I could see the result, it did print to the console.

Do you have any hint how to modify the code without the msg.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Jon & Cordell,

I modified the code, it is working for this part now.
I will post a short MSG of this all program later.
Thanks!

using System;
using System.IO;
using System.Text;

class OpenText
{
public static void Main()
{

StreamReader sr = new StreamReader("c:\\USIndex\\_A1DOWM.txt");

string currentLine = null;


currentLine = sr.ReadLine();

currentLine = sr.ReadLine();

while(currentLine != null)
{
string[] data = currentLine.Split(',');

string val1 = data[0];
string val2 = data[1];
// etc.
Console.WriteLine(val1);
Console.WriteLine(val2);

currentLine = sr.ReadLine();
}


}
}
 
Back
Top