CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: text file

  1. #1
    Join Date
    Jul 2005
    Posts
    22

    text file

    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

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: text file

    Maybe you could do it like this:
    Code:
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured