Click to See Complete Forum and Search --> : text file


dave1979bell
October 13th, 2005, 08:18 AM
I have a text file that looks like the following...


Name | Title
Dave | Mr
Mark | Sir
John | Dr

I need to open the file (c:\list.txt) and add each line(witch will vary) to a string. so i will end up with an array of strings. and when someone adds a person to the list the program when run again will be able to add an extra plac in the array.

I basicly need the reading the lines of a file into an array part.

can some one please help?


Tanks in advance,


David Bell

wildfrog
October 13th, 2005, 08:39 AM
Maybe you could do it like this:

static void Main(string[] args)
{
System.Collections.ArrayList pArray = new ArrayList();
System.Collections.Hashtable pHash = new Hashtable();

// open stream reader
System.IO.StreamReader pSR = new StreamReader("test.txt");

string strLine = null;
while ((strLine = pSR.ReadLine()) != null)
{
// add the line to an array
pArray.Add(strLine);

// or split the string and add the part into a hash table
string[] strParts = strLine.Split('|');
string strName = strParts[0];
string strTitle = strParts[1];
pHash.Add(strName, strTitle);
}

// close the stream reader
pSR.Close();
}

You should ofcourse do some error checking.

-petter