Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Hashtable read text file???

Posted on 2004-08-23
6
239 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding  DYMO Labelprinter to c# client application 4 85
PrintingFoundIt(Almost!(Damn!)) 1 30
Cant save 3D 4 23
C# Desktop Application 3 54
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 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