Solved

Hashtable read text file???

Posted on 2004-08-23
6
236 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now