Solved

Hashtable read text file???

Posted on 2004-08-23
6
238 Views
Last Modified: 2012-06-27
I've searched all my books and the Internet and cannot find a good example to read a text file into a hashtable. My text file is tab delimited as such below)and I need to load them into a hashtable as a DNS cashe...... Help.........

123.123.123.123      www.mywebsite.com
111.222.333.444      www.nowebsite.com
0
Comment
Question by:kvnsdr
  • 3
  • 2
6 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11876438

    static Hashtable ReadFromFile(string filename)
    {
    StreamReader SR;
    string S;
    Hashtable ret = new Hashtable
    SR=File.OpenText(filename);
    S=SR.ReadLine();
    while(S!=null)
    {
         string [] tmp = S.Split("\t");
         if(tmp.Count == 2) {
              if(!ret.Contains(tmp[0]) {
                    ret.add(tmp[0], tmp[1])
              } else {
                    how do you want to handle duplicate entries ?
              }
         } else {
              throw new system.exception("bad data");
         }
    S=SR.ReadLine();
    }
    SR.Close();
    }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11876445
btw this will most likely require a little editing no compiler here this sec so just typed here.
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 11876915

  I like the coding. I have an error in my code that says:

"Index was outside the bounds of the array"

All I changed was SR = File.OpenText("MyFile.txt");  

????????????????
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 4

Accepted Solution

by:
somnatic earned 500 total points
ID: 11878436
i hope gregoryyoung won't kill me for editing his code ;)
try this .. works for me .. and .. don't forget the using System.IO; Statement



            private void button1_Click(object sender, System.EventArgs e)
            {
                  Hashtable r = ReadFromFile("d:\\testtab.txt");
            }
            static Hashtable ReadFromFile(string filename)
            {
                  StreamReader SR;
                  string S;
                  Hashtable ret = new Hashtable();
                  SR=File.OpenText(filename);
                  S=SR.ReadLine();
                  while(S!=null)
                  {
                        string [] tmp = S.Split('\t');
                        if(tmp.Length == 2)
                        {
                              if(!ret.Contains(tmp[0]) )
                              {
                                    ret.Add(tmp[0], tmp[1]);
                              }
                              else
                              {
                              }
                        }
                        else
                        {
                              throw new System.Exception("bad data");
                        }
                        S=SR.ReadLine();
                  }
                  SR.Close();

                  return ret;
            }
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11888402
jeeze forget a return statement typing without a compiler and lose all credit ... nice one.
0
 
LVL 4

Expert Comment

by:somnatic
ID: 11889478
you forgot the semicolon somewhere at the beginning ;) ..

any way to shift points ?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question